Skip to main content

2 docs tagged with "interior-mutability"

View all tags

Combining Rc<T> and RefCell<T> for Multiple Owners of Mutable Data

We've seen how Rc enables multiple owners and how RefCell allows interior mutability. Individually, they solve specific problems. But when combined, they unlock a powerful pattern for managing shared, mutable state in a single-threaded context. The combination Rc> allows a single piece of data to have multiple owners, any of whom can mutate it.

RefCell<T> and the Interior Mutability Pattern

We've seen how Rust's borrowing rules are enforced at compile time, and how Rc allows shared ownership of immutable data. But what if we need to mutate data that is shared and theoretically immutable? For this, Rust provides the interior mutability pattern, and its primary tool is the RefCell smart pointer.