3 ms·
This is interesting. I did `clone()` everywhere rather than `Rc<>` I wonder how these approaches compare?
by throwaway894345 6y ago
This is interesting. I did `clone()` everywhere rather than `Rc<>` I wonder how these approaches compare?
- Klasiaster 6y agoFor the read-only case it's the same except for a larger overhead of the cloning. But for the case where the data is modified, Rc often goes with RefCell as Rc<RefCell<X>> and allows in-place mutation called interior mutability. This means that the other code that got the copy of the Rc will observe the write effects.
- steveklabnik 6y agoOn most things, clone() will make a full copy. If your type is wrapped in Rc<T>, then calling clone() on it will increase a reference count. So it ends up being cheaper. If you were mutating things, though, you'll need extra stuff in the Rc<T> case, though. If you're mutating in the regular case, you'd be changing the copies, of course, so the originals wouldn't change.