On this page
RefCell
Prev: `Rc` | Up: smart_pointers | Next: `Cow`
Rel: smart_pointers, rust/builtins
Ref: Rust std — RefCell
Tags: #public #prog #rust #stdlib #smart_pointers #tier/standard
Signature & Overview
RefCell<T>
Single-threaded interior mutability enforcing borrow rules at runtime (.borrow() / .borrow_mut()).
Notes & Pitfalls
- Runtime-checked interior mutability (single-threaded): Violating aliasing rules (
borrow_mut()while another borrow is active) panics at runtime—usetry_borrow_mut()if unsure. - Commonly paired as
Rc<RefCell<T>>for graphs or observer trees; keep borrow guards scoped tightly.
Example
use std::cell::RefCell;
fn main() {
let log = RefCell::new(Vec::new());
log.borrow_mut().push("started");
assert_eq!(log.borrow().as_slice(), &["started"]);
}
Linked from
Related notes
- tuts/rust/std/smart_pointers/Cow Linked here; links to this page; shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/smart_pointers/Rc Linked here; links to this page; shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/smart_pointers Linked here; links to this page; shared tags: #prog, #rust, #stdlib
- tuts/rust/builtins Linked here; shared tags: #prog, #rust
- tuts/rust/std/collections/BinaryHeap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/BTreeMap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/HashMap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/HashSet shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/VecDeque shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/error_and_fmt/Display shared tags: #prog, #rust, #stdlib, #tier