Cell
Reference Video | Rust doc - cell (module), Cell (struct)
- Allows mutable sharing
- No one has a reference to the thing inside a cell. so anyone can modify the cell itself
- Does not implement
Sync - can get the value only if:
- you have a mutable reference to it (at which point you dont really need a cell)
- the type inside implements
Copy
- therefore a cell is usually used for small
Copytypes - useful for when you want multiple things referencing the same thing in a single threaded system
- Technically all the magic of a
Cellis done int theUnsafeCellimplementation The compiler has extra info aboutUnsafeCelland considers it differently. This allows for such behaviour. UnsafeCellimplements!Syncand a bunch of other featuresUnsafeCellalso allows us to get an exclusive, mutable reference from a shared reference. This is allowed because the compiler trusts the guy :)