3 ms·
I've written a fair amount of Rust and my beginner experience was not as positive as yours. It took me a very long time to fully deal with things like the borro
by afavour 6y ago
I've written a fair amount of Rust and my beginner experience was not as positive as yours. It took me a very long time to fully deal with things like the borrow checker.
My personal recommendation to anyone just writing code as a learning exercise is that they start with using Rc<> wherever a borrow checker-related issue arises. Then you can get used to the rest of Rust. Once you feel a lot more proficient you can go back and start replacing Rc<> with references (and you might find that you don't even need to in many instances anyway)
- throwaway894345 6y agoThis 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.