Introduction
Interior mutability is a design pattern in Rust that allows you to
mutate data even when there are immutable references to that data;
normally, this action is disallowed by the borrowing rules. To mutate
data, the pattern uses unsafe
code inside a data structure to bend
Rust’s usual rules that govern mutation and borrowing. We can use types
that use the interior mutability pattern when we can ensure that the
borrowing rules will be followed at runtime, even though the compiler
can’t guarantee that. The unsafe
code involved is then wrapped in a
safe API, and the outer type is still immutable.
Reason to choose RefCell<T>
Because RefCell<T>
allows mutable borrows checked at runtime, you can
mutate the value inside the RefCell<T>
even when the RefCell<T>
is
immutable.
Example
This won’t compile:
This will: