References (& and &mut) (Rust)
A reference is a pointer to a value that you can read (&T) or modify (&mut T) without owning it. References are guaranteed by the compiler to always point to valid data.
Rust
Writing & before a value creates an immutable reference; &mut creates a mutable one. You dereference with * to read or write the pointed-to value, though Rust auto-dereferences in most method and field access.
Unlike raw pointers in C, Rust references can never be null and can never dangle — the borrow checker proves at compile time that a reference cannot outlive the data it points to. That is why Rust has no segfaults in safe code.
References are the mechanism behind borrowing. The distinction matters: &T means 'shared, read-only', &mut T means 'exclusive, read-write'. You can have many &T or one &mut T, but not both simultaneously.
Syntax
let r = &value; // immutable reference
let m = &mut value; // mutable reference
*m = 5; // write through itExamples
Rust
fn main() {
let mut n = 10;
let r = &mut n;
*r += 5;
println!("{}", n);
}Output:
15