4 ms·
In addition to what BurntSushi said, it's worth thinking of the interaction as `&mut T` allows unsynchronized mutation. Because it's guaranteed to be unique, no
by Measter 4y ago
In addition to what BurntSushi said, it's worth thinking of the interaction as `&mut T` allows unsynchronized mutation. Because it's guaranteed to be unique, nothing else in the program can access the data behind the reference, so you don't need any synchronization to prevent data races.
With a `&T` on the other hand, the entire point is to allow more than one to exist. That means that arbitrarily many parts of the program could be accessing the data at the same time. If you allow unsynchronized mutation in that situation, you can end up with data races and mangled data from those different parts making changes at the same time.
That's where types such as the single-threaded `Cell` and `RefCell`, and the thread-safe `Mutex` and `RwLock` come in. They force you to go through them, and therefore synchronization, in order to mutate.