📝In Rust, mutable references never alias

In Rust, two mutable references never point to the same location in memory—they never alias.

There is no safe way to create two &mut references to the same value, and creating two mutable references via unsafe code is undefined behavior. Which basically guarantees that if you have a &mut, it is exclusive. (In Rust, mut/const references are more about exclusive/shared access).

Because Aliasing only matters if a reference is aliased with another mutable reference, this allows compiler to perform more optimizations.

See also

Backlinks

Want to receive my 🖋 posts as I publish them?