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 an apparently immutable Rust value can retain changing internal state, especially alongside reference-counted ownership.
- How deep it goes
- Defines the property and compares wrapper choices and runtime costs, without detailing their implementations.
- Examples
- Illustrates changing Rc<T> and Arc<T> reference counts and the composed type Rc<RefCell<T>>.
- What you can do after
- Apply it via Cell<T> or RefCell<T> when an immutable interface must accommodate changing state, while accounting for 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
- Enables mutation inside immutable structures when mutable-reference borrowing is too restrictive.
- How deep it goes
- Distinguishes Cell from RefCell and identifies threaded alternatives, but stops short of detailing their borrowing mechanisms.
- Examples
- Uses a doubly linked list with Rc<RefCell<ListItem<T>>> links and boxed data.
- What you can do after
- Apply it via RefCell with Rc for suitable single-threaded data structures, or Arc with synchronization for multithreaded access.
Read how Code Like a Pro in Rust explains interior mutability →
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.
- rwlockuses · An RwLock is a synchronization primitive that permits multiple concurrent readers or one exclusive writer for protected data.
- lockinguses · Locking is a synchronization technique that restricts concurrent access to shared resources or protected operations.
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.


