Box = single owner, heap allocation, no runtime overhead. Rc = multiple owners, heap allocation, reference counting overhead. Choose Box when one owner suffices; choose Rc when multiple owners are needed.
Box — simple, single ownership:
1// Recursive type — only possible with Box2enum List {3 Cons(i32, Box<List>),4 Nil,5}67let list = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
Rc — shared ownership:
1use std::rc::Rc;23struct Node {4 value: i32,5 parent: Option<Rc<Node>>, // Multiple children can share parent6}78let parent = Rc::new(Node { value: 1, parent: None });9let child1 = Node { value: 2, parent: Some(Rc::clone(&parent)) };10let child2 = Node { value: 3, parent: Some(Rc::clone(&parent)) };11// Both children share the same parent node
When to use Box:
When to use Rc:
Common mistakes: