Skip to main content
The &mut operator creates a mutable reference to a value, allowing a binding to read and modify data without taking ownership of it. In Rust’s ownership system, this operation is formally known as a “mutable borrow.” Under the hood, &mut T is a non-null, aligned pointer to a memory address containing a value of type T. Unlike raw pointers (*mut T), the Rust compiler statically verifies the lifetime of a &mut reference to guarantee it never points to freed or invalid memory.

Syntax and Type Signatures

The &mut syntax is used in two distinct contexts: as an expression to create the reference, and in type signatures to define the reference type.

Core Mechanics and Compiler Rules

The behavior of &mut is strictly governed by Rust’s Borrow Checker to ensure memory safety and prevent data races at compile time. 1. Base Mutability Requirement You cannot create a mutable reference to an immutable binding. The underlying data must be explicitly declared with the mut keyword.
2. Strict Exclusivity (No Aliasing) Rust enforces a strict “aliasing XOR mutation” rule. If a &mut reference to a piece of data exists, it must be the only active reference to that data in the current scope. You cannot simultaneously hold multiple &mut references, nor can you hold a &mut reference alongside an immutable & reference to the same data.
3. Explicit Dereferencing To mutate the underlying data that the &mut reference points to, you must use the dereference operator (*). This instructs the compiler to follow the pointer to the actual memory location.
Note: Rust provides implicit dereferencing (auto-deref) when calling methods on a mutable reference (e.g., reference.method()), but direct assignment or arithmetic requires the explicit * operator. 4. Move Semantics and Reborrowing Because mutable references must guarantee strict exclusivity, &mut T does not implement the Copy trait. A standard assignment of a mutable reference to another variable moves the reference, invalidating the original binding.
To use multiple mutable references to the same data sequentially without losing the original reference, Rust utilizes a mechanism called reborrowing. A reborrow creates a new, temporary &mut reference with a shorter lifetime, freezing the original reference until the reborrow expires. Implicit reborrowing occurs only at coercion sites, such as when passing a mutable reference to a function argument or when an explicit type annotation is provided during assignment. If a coercion site is not present, you must perform an explicit reborrow using &mut *.
Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More