I just learned something that may be obvious to Rust experts
Binding Mutability vs Reference Mutability
A crucial distinction in Rust is that binding mutability and reference mutability are independent concepts. They operate on different levels and do not depend on each other.
Binding mutability (controlled by let mut) determines whether you can reassign the variable to hold a different value. Reference mutability (controlled by & vs &mut) determines whether a reference has permission to modify the data it points to.
These two properties are orthogonal: knowing that a binding is mutable tells you nothing about what type of reference the & operator will create.
I was a bit confused about:
If
s is a mutable bindingthen why is&snot areference to a mutable binding?
fn main() {
let mut s = String::from("hello");
let r1 = &s;
}
But now I understand.