An explanation of Rust pointers, assuming familiarity with C or Go, but seeking the Rust way of thinking.
In C or Go, a pointer is like a scrap of paper with a house address on it.
In Rust, a pointer is still a memory address, but it comes attached to a legal contract.
Rust doesn't just care where the data is; it cares who is allowed to touch it and for how long.
&T and &mut TThese are the most common "pointers" in Rust. They are purely for Borrowing.
int *p, but the compiler tracks the scope of the variable p points to.&T (Shared Reference): You can give this to many people. They can all look at the data, but nobody can change it.&mut T (Mutable Reference): You can only give this to one person at a time. While they have it, not even you can look at the data. This prevents race conditions (two people writing at once).&T — Read-Only Keycard: