Interior mutability
Interior mutability is a programming mechanism that permits mutation through an otherwise shared or immutable access path while enforcing safety through specialized checks or synchronization.
Rust in Action: Systems programming concepts and techniques
Interior mutability is a design property allowing a value's internal state to change through an otherwise immutable interface.
- How it is used
- Explains how Rust types retain changeable internal state behind an immutable interface.
- How deep it goes
- Defines the property and sketches wrappers and reference counts without detailing their implementations.
- Examples
- Uses changing reference counts in Rc and Arc and the combined type Rc<RefCell<T>> as examples.
- What you can do after
- Evaluate/understand when an interior-mutability wrapper can add flexibility at a runtime cost.
Read how Rust in Action: Systems programming concepts and techniques explains interior mutability →
Idiomatic Rust
Interior mutability is a capability that permits mutation through an otherwise shared reference while enforcing access rules through a runtime or specialized checking mechanism.
- How it is used
- Enables mutation of data inside an immutable container when compiler-enforced inherited mutability is too restrictive.
- How deep it goes
- Defines runtime enforcement and contrasts Cell replacement with mutable access through RefCell and OnceCell.
- Examples
- Demonstrates appending to a String through RefCell::borrow_mut after moving the string into the container.
- What you can do after
- Apply interior mutability with RefCell to modify owned data behind an immutable binding.
Code Like a Pro in Rust
Interior mutability is a programming pattern in which a value permits mutation through a shared or otherwise nonmutable handle while enforcing its safety rules at runtime or through synchronization.
- How it is used
- Uses interior mutability to enable mutation inside immutable structures when mutable references are too restrictive.
- How deep it goes
- Contrasts RefCell borrowing with Cell value movement and identifies synchronized alternatives for multithreaded use.
- Examples
- Illustrates shared interior mutability with a doubly linked list using Rc, RefCell, and Box.
- What you can do after
- Apply interior mutability using Cell or RefCell for single-threaded cases, or synchronization primitives with Arc for shared multithreaded cases.
Read how Code Like a Pro in Rust explains interior mutability →
Also covered in
- Learn Rust in a Month of Lunches
- Embedded Software with RustIn early access (MEAP)
What to know first
- mutable referenceA mutable reference is a reference or handle that provides access to a value or storage location that can be modified, subject to the language’s access rules.
- referenceA reference is an indirect value, binding, or relationship that identifies or provides access to another object, value, resource, or source.
- atomicityAtomicity is the property that an operation or group of operations takes effect as an indivisible whole, without observable partial effects.
- copyCopying is the creation of a separate representation of an existing value, object, or data, preserving some or all of its contents or state.
Related concepts
- refcellused by · RefCell is a Rust standard-library container that provides interior mutability by checking borrowing rules at runtime.
- cellused by · Cell is a Rust standard-library type that provides interior mutability by allowing values to be retrieved or replaced through shared access without exposing references to its contents.
- lockinguses · Locking is a synchronization technique that controls concurrent access to a shared resource or protected operation.
- rwlockuses · An RwLock is a synchronization primitive that permits multiple concurrent readers or one exclusive writer to access protected data.
Questions about interior mutability
What is interior mutability?
Interior mutability is a programming mechanism that permits mutation through an otherwise shared or immutable access path while enforcing safety through specialized checks or synchronization.
Which Manning books explain interior mutability?
Rust in Action: Systems programming concepts and techniques, Idiomatic Rust and Code Like a Pro in Rust.
What should I know before learning interior mutability?
Manning books that teach interior mutability usually introduce mutable reference, reference, atomicity and copy first.
What is the difference between interior mutability and inherited mutability?
Interior mutability is a programming mechanism that permits mutation through an otherwise shared or immutable access path while enforcing safety through specialized checks or synchronization. Inherited mutability is Rust's rule that access to nested data derives its mutability from the binding or reference through which the data is reached.
What kinds of interior mutability are there?
Kinds of interior mutability include vcell.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


