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

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

What to know first

Related concepts

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.