Thinking in Rust: Ownership, Access, and Memory Safety
ยท 9 min read
I'm an experienced C++ programmer, but still feel that I need a mindset shift when starting to work with Rust.
- References can be immutable (
&
) or mutable (&mut
), which is straightforward for simplest cases. There are more complex cases, e.g. there are data types with interior mutability (e.g.RefCell
), there are multi-level references (& &mut
). - Rust also models memory safety in multi-thread scenarios, so there's
Send
andSync
. They have intricate rules related to various types of references, e.g. when&T
isSend
orSync
? How about&mut T
? - There are various data types, like
Rc
,Arc
,Cell
,RefCell
,Mutex
,RwLock
,Cow
. When to pick which?