Box, Rc, Arc & RefCell in Rust

From the Rust cheat sheet · Smart Pointers · verified Jul 2026

Box, Rc, Arc & RefCell

Heap allocation, reference counting, and interior mutability.

rust
// Box<T> — heap allocation
let b = Box::new(5);
// Used for: recursive types, large data, trait objects

// Rc<T> — multiple owners (single-threaded)
use std::rc::Rc;
let a = Rc::new(String::from("hello"));
let b = Rc::clone(&a);  // Both a and b own the data

// Arc<T> — multiple owners (thread-safe)
use std::sync::Arc;
let data = Arc::new(vec![1, 2, 3]);

// RefCell<T> — interior mutability (runtime borrow checking)
use std::cell::RefCell;
let cell = RefCell::new(5);
*cell.borrow_mut() += 1;
💡 Box is for heap allocation — use it for recursive types, large data, and dyn Trait
⚡ Rc is for multiple owners in one thread; Arc is the thread-safe version — same API
📌 RefCell moves borrow checking from compile time to runtime — panics if rules are violated
🟢 Cow<str> avoids cloning when you might or might not need to modify a string
boxrcarcrefcellcowsmart-pointers
Back to the full Rust cheat sheet