Skip to content

Interior mutability

Category: Safety in languages · Status: stub

One line: Changing data behind a shared reference through a type that enforces the rules itself — Cell and RefCell within one thread, Mutex and atomics across threads.

How it connects

flowchart LR
  n_atomic_variable["Atomic variable"]
  n_interior_mutability["Interior mutability"]
  n_mutex["Mutex"]
  n_interior_mutability -->|uses| n_atomic_variable
  n_interior_mutability -->|uses| n_mutex
  classDef center stroke-width:3px
  class n_interior_mutability center
  classDef outside stroke-dasharray: 4 3
  class n_atomic_variable,n_mutex outside

In each language

Rust std::cell: Cell, RefCell and OnceCell are single-threaded and not Sync; across threads use Mutex, RwLock or atomics
C++ The mutable specifier lets a member change inside a const object — typically a mutex, as in the page's rule that mutable and mutex go together

Where to read more