Coding/Learn/rust/mutable references
Rust Foundational Course
01.Introduction to Rust & Memory Safety
02.Formatting, Comments & Macros (println!)
03.Variables, Mutability & Shadowing
04.Scalar & Compound Types (Integers, Floats, Tuples, Arrays)
05.Functions, Parameters & Return Expressions
06.Conditionals & If as an Expression
07.Ownership Rules & Move Semantics
08.References & Borrowing (&T)
09.Mutable References (&mut) & Aliasing XOR Mutability
10.Slices & String Types (&str vs String)
11.Loops in Rust (loop, while, for)
12.Pattern Matching with match Expressions
13.Structs, Associated Functions & Methods (impl)
14.Enums & the Option<T> Type (No Null!)
15.Concise Pattern Matching (if let & let else)
16.Vectors & Growable Collections (Vec<T>)
17.Error Handling with Result<T, E> & the ? Operator
18.Traits, Shared Behavior & Generics
19.Closures & Functional Iterators (map, filter, collect)
20.Capstone: High-Performance In-Memory Log Telemetry Engine
Course Certificate0%

Complete all lessons to earn

LearnMutable References (&mut) & Aliasing XOR Mutability
9 / 20 0%
Lesson 09 of 20
6 mins read

Mutable References (&mut) & Aliasing XOR Mutability

PreviousNext Lesson
In Plain English

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.

The Mental Model

"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.

PointCore Rules to Remember

Point star
&mut T: Grants exclusive write access to borrowed data.
Point star
Single Writer Rule: Only ONE mutable reference permitted at a time.
Point star
No Data Races: Eliminates race conditions before code ever reaches production.

Common Beginner Traps & How to Fix Them

Mistake: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.

Pro Tip: Rust’s single-writer rule makes multi-threaded concurrency fearless—data races are mathematically impossible.

Live Interactive Example

Hit Run Code to see it live
Modifying via Mutable References
Rust 1.78 / rustc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: append_tag mutates the caller’s heap buffer directly via exclusive &mut reference.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Mutate a counter via &mut i32

  • PointWrite fn double_val(num: &mut i32) { *num *= 2; }
  • PointIn main(), let mut score = 25;
  • PointCall double_val(&mut score) and print "Doubled Score: 50".
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates