Cell and RefCell
Quick answer
Interior mutability permits certain controlled changes through a shared reference. Cell exchanges whole values; RefCell checks borrowing rules while the program runs. Both are intended for single-threaded shared access.
Choose the correct borrowing model
Cell::get works for Copy values and set replaces the contents. RefCell::borrow and borrow_mut return guards; overlapping mutable/shared access fails at runtime. Use try_borrow_mut to handle a conflict without panicking. End a guard's lifetime before taking a conflicting borrow.
use std::cell::Cell;struct Counter(Cell<u32>);impl Counter { fn tick(&self) { self.0.set(self.0.get() + 1); } }fn main() { let c = Counter(Cell::new(0)); c.tick(); c.tick(); println!("{}", c.0.get()); }Expected stdout (not executed):
2The method uses the safe Cell interface without an exclusive reference.
use std::cell::RefCell;fn main() { let values = RefCell::new(vec![1]); let guard = values.borrow(); println!("conflict: {}", values.try_borrow_mut().is_err()); drop(guard); values.borrow_mut().push(2); println!("{:?}", values.borrow()); }Expected stdout (not executed):
conflict: true[1, 2]After the first guard ends, a mutable borrow succeeds.
Common mistake
RefCell does not remove borrowing rules; it moves enforcement to runtime.
Try it yourself
Use Rc<RefCell<Vec<i32>>> to give two local owners controlled access to one list.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cell and RefCell — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A RefCell borrow guard remains in scope while you need mutable access. What should happen first?
Cell and RefCell — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::cell::Cell; struct Counter(Cell<u32>); impl Counter { fn tick(&self) { self.0.set(self.0.get() + 1); } } fn main() { let c = Counter(Cell::new(0)); c.tick(); c.tick(); println!("{}", c.0.get()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Cell and RefCell — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::cell::RefCell; fn main() { let values = RefCell::new(vec![1]); let guard = values.borrow(); println!("conflict: {}", values.try_borrow_mut().is_err()); drop(guard); values.borrow_mut().push(2); println!("{:?}", values.borrow()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.