Cell provides interior mutability: it lets code change data inside an otherwise immutable structure by moving values in and out, rather than exposing references to its contents. It is a safe escape hatch when ordinary mutable borrowing is too restrictive. section 5.6
Choose the interior-mutability tool to match the access pattern:
- Use Cell when moving values in and out is suitable. Prefer RefCell when you need to borrow references to the stored data. section 5.6
- Use either sparingly. If it mainly serves to get around the borrow checker, reconsider the data structure. section 5.6
- For access across threads, use synchronization such as Mutex or RwLock, typically with Arc. section 5.6
When Cell is not the fit
A doubly linked list with shared node ownership uses Rc<RefCell<ListItem<T>>>. Its nodes need mutable access through shared pointers, so the implementation uses RefCell’s reference-borrowing behavior rather than Cell’s value movement. section 5.6
