liveBook · Manning

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.

How 3 Manning books explain it

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.

Read how Idiomatic Rust explains interior mutability →

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

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.