Python Mutable References (&mut) & Aliasing XOR Mutability
Mutable references (&mut T) allow you to modify borrowed data. Rust enforces the fundamental Aliasing XOR Mutability rule: you can have either one mutable reference OR any number of immutable references, but never both at the same time.
"Think of a shared Google Doc: either dozens of people can read it simultaneously (immutable references), or one single editor holds the write lock (&mut). You can never write while others are actively reading."
Deep Dive: How It Works
Mutable Borrow: let r = &mut value; allows modifying the underlying data.
Data Race Prevention: Having simultaneous readers and writers causes data races. Rust prevents this at compile time.
Non-Lexical Lifetimes (NLL): A reference’s lifetime ends at its last use, not at the end of the enclosing block.
Syntax Blueprint
let mut s = String::from("Hi"); let r = &mut s; r.push_str("!");Only one &mut reference to s can exist in any overlapping scope.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Borrowing as mutable when already borrowed as immutable.Why it happens: let r1 = &s; let r2 = &mut s; println!("{}", r1); fails with E0502: cannot borrow as mutable because it is also borrowed as immutable.
How to fix: Ensure all immutable borrows are finished before creating a mutable borrow.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Mutate a counter via &mut i32
Write fn double_val(num: &mut i32) { *num *= 2; }
In main(), let mut score = 25;
Call double_val(&mut score) and print "Doubled Score: 50".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.