What is the difference between a mutable and an immutable reference in Rust?
Learn the difference between &T and &mut T in Rust: many readers or one writer, how the borrow checker enforces it, and why it stops data races at compile time.
Expected Interview Answer
An immutable reference (&T) lets you read a value without changing it, and you can have many at once; a mutable reference (&mut T) lets you modify the value, but the borrow checker allows only one at a time and none alongside it.
Rust enforces these rules at compile time to guarantee memory safety without a garbage collector. The core invariant is 'aliasing XOR mutability': you may have many readers or exactly one writer, but never both simultaneously. This prevents data races and iterator invalidation before the program even runs, so a value cannot be mutated through one path while being observed through another.
- Prevents data races at compile time
- Allows many concurrent readers safely
- Guarantees a single writer with exclusive access
- Eliminates iterator invalidation bugs
- No runtime cost — checks happen during compilation
AI Mentor Explanation
Think of the match ball during play. Many fielders may look at it, study its shine and read the seam at the same time — that is like many immutable references. But only the bowler polishing or scuffing it may alter its condition, and while he works on it nobody else may touch or change it, exactly like a single exclusive mutable reference blocking all other access.
Step-by-Step Explanation
Step 1
Take a shared borrow
Use &value to create an immutable reference; you can read through it but not mutate the underlying data.
Step 2
Take an exclusive borrow
Use &mut value to create a mutable reference; the variable itself must be declared mut for this to be allowed.
Step 3
Apply the borrow rules
At any point you may have either any number of &T or exactly one &mut T, but never both at the same time.
Step 4
Respect scopes and NLL
A borrow lasts until its last use (non-lexical lifetimes), so a mutable borrow can begin once earlier immutable borrows are no longer used.
Step 5
Let the compiler verify
The borrow checker rejects any code that violates aliasing-XOR-mutability before the program runs — no runtime checks needed.
What Interviewer Expects
- Clear statement of the aliasing-XOR-mutability rule
- Knowing many &T or one &mut T are allowed, never both
- Awareness that checks are compile-time with zero runtime cost
- Understanding this prevents data races and iterator invalidation
- Mention of non-lexical lifetimes shortening borrow scopes
Common Mistakes
- Claiming you can have multiple mutable references at once
- Forgetting the owning variable must be declared mut for &mut
- Thinking borrow checks add runtime overhead
- Confusing an immutable reference with a const/immutable value
- Not realizing a borrow ends at its last use, not end of block
Best Answer (HR Friendly)
“In Rust, an immutable reference is a read-only view of some data, and you can share many of them at once. A mutable reference lets you change the data, but only one can exist at a time, which is how Rust stops bugs where two parts of a program fight over the same value.”
Code Example
fn main() {
let mut score = 42;
// Many immutable references are fine
let r1 = &score;
let r2 = &score;
println!("{} {}", r1, r2); // last use of r1, r2 here
// Now a single mutable reference is allowed
let m = &mut score;
*m += 1;
println!("{}", m);
// ERROR if uncommented: cannot borrow `score` as mutable
// while it is also borrowed as immutable
// let bad_r = &score;
// let bad_m = &mut score;
// println!("{} {}", bad_r, bad_m);
}Follow-up Questions
- What is non-lexical lifetime (NLL) and how does it relax borrow scopes?
- How does &mut prevent data races in multithreaded code?
- What is interior mutability and how do RefCell and Cell fit in?
- Can you return a mutable reference from a function? What are the constraints?
- How do reborrows let you pass a &mut to another function without moving it?
MCQ Practice
1. How many mutable references to the same value can exist simultaneously in safe Rust?
The borrow checker allows at most one &mut T at a time, and none alongside any immutable references, to guarantee exclusive write access.
2. Which combination is allowed by the borrow checker at the same point in time?
Any number of immutable references may coexist, but a mutable reference requires exclusive access with no other references active.
3. When are Rust's borrow rules checked?
Borrow checking happens during compilation, so violations are caught before the program runs with zero runtime cost.
Flash Cards
What does &T represent? — An immutable (shared) reference — read-only access, and many can exist at once.
What does &mut T represent? — A mutable (exclusive) reference — read/write access, but only one at a time and no other references alongside it.
State Rust's core borrow rule. — Aliasing XOR mutability: many readers or one writer, never both simultaneously.
What runtime cost do borrow checks add? — None — they are enforced entirely at compile time.
Why must the owner be declared mut for &mut? — You can only take a mutable reference to a value that is itself mutable.