Mutable references
Quick answer
A mutable reference lets a function modify a borrowed value without owning it. Use &mut value for the argument and &mut T for the parameter type.
Exclusive access
For the duration of a mutable borrow, access to the borrowed part must respect exclusivity: competing shared or mutable access is not allowed. The original binding generally needs to be mutable for this form of borrow. Assign through *reference to modify the referent, not to change the reference itself.
fn increment(counter: &mut i32) { *counter += 1; }fn main() { let mut count = 4; increment(&mut count); println!("{count}");}Expected stdout (not executed):
5The caller's original integer changes.
fn emphasize(text: &mut String) { text.push('!'); }fn main() { let mut note = String::from("go"); emphasize(&mut note); println!("{note}");}Expected stdout (not executed):
go!The owner remains note; the function has temporary exclusive access.
Common mistake
Do not use a shared reference when the operation must change ordinary data. A mutable binding alone does not grant mutation through &T.
Try it yourself
Write fn reset(value: &mut i32) that replaces an integer with zero and test it.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Mutable references — practice quiz
Choose one answer per question, then review the explanation.
Given `let mut n=2; let r=&mut n; *r=6;`, what is n after the borrow ends?
Which binding permits taking an ordinary mutable borrow of its integer?
Why cannot a second overlapping mutable borrow access the same value while the first is still used?