Lesson 21 of 26
Shared ownership: Rc, RefCell and Weak
In Python, sharing is the default. Any number of names, lists and objects can point at the same object, and any of them can change it. CPython keeps a reference count to know when to free it.
In Rust, a value has one owner (lesson 13). Usually that is enough. When it isn't, as with a graph, a tree with links back to the parent, or a cache that several parts of a program hold on to, Rust gives you Python's model back, one piece at a time:
Rc<T>("reference counted"): many owners of one value, freed when the last owner goes. That is what CPython does for every object.Rconly lets you read.RefCell<T>: lets you change a value through a shared reference, by checking the borrow rules while the program runs instead of when it compiles.Weak<T>: a reference that doesn't keep the value alive, like Python'sweakref.
When reading code, Rc<RefCell<T>> means "a shared object that anyone holding it may change".
That is a normal Python object.
Rc: many owners of one value
Two teams share one roster. In Python both attributes simply point at the same list. In Rust the roster
is wrapped in an Rc, and each team holds one of its owners.
class Team:
def __init__(self, name, roster):
self.name = name
self.roster = roster
roster = ["ada", "bob"]
red = Team("red", roster)
blue = Team("blue", roster)
shared = "yes" if red.roster is blue.roster else "no"
print(f"{red.name} and {blue.name} share {len(red.roster)} players: {shared}")use std::rc::Rc;
struct Team {
name: String,
roster: Rc<Vec<String>>,
}
fn main() {
let roster = Rc::new(vec![String::from("ada"), String::from("bob")]);
let red = Team { name: String::from("red"), roster: Rc::clone(&roster) };
let blue = Team { name: String::from("blue"), roster: Rc::clone(&roster) };
let shared = if Rc::ptr_eq(&red.roster, &blue.roster) { "yes" } else { "no" };
println!("{} and {} share {} players: {shared}", red.name, blue.name, red.roster.len());
}red and blue share 2 players: yesChanging shared data: Rc<RefCell<T>>
Python lets everyone who holds an object change it. Rc alone only gives shared reading,
because "many readers or one writer" (lesson 13) still applies. RefCell lets you change the value anyway,
and checks that rule while the program runs: .borrow() to read, .borrow_mut() to change.
class Settings:
def __init__(self):
self.theme = "light"
shared = Settings()
editor = shared
viewer = shared
editor.theme = "dark"
print(f"viewer sees: {viewer.theme}")use std::cell::RefCell;
use std::rc::Rc;
struct Settings {
theme: String,
}
fn main() {
let shared = Rc::new(RefCell::new(Settings { theme: String::from("light") }));
let editor = Rc::clone(&shared);
let viewer = Rc::clone(&shared);
editor.borrow_mut().theme = String::from("dark");
println!("viewer sees: {}", viewer.borrow().theme);
}viewer sees: darkCycles: Python's gc cleans up, Rc leaks
Two objects that point at each other never reach a reference count of 0. CPython's cycle collector finds
them and frees them; gc.collect() runs it right now. Rust has no cycle collector. Two Rcs that
point at each other keep each other alive until the program ends: a memory leak. It compiles fine, because a leak
is wasteful but not unsafe. That is why the two outputs differ: Rust never prints free.
import gc
class Node:
def __init__(self, name):
self.name = name
self.other = None
def __del__(self):
print(f"free {self.name}")
a = Node("a")
b = Node("b")
a.other = b
b.other = a
del a, b
print("names deleted")
gc.collect()
print("after gc.collect()")use std::cell::RefCell;
use std::rc::Rc;
struct Node {
name: String,
other: RefCell<Option<Rc<Node>>>,
}
impl Drop for Node {
fn drop(&mut self) {
println!("free {}", self.name);
}
}
fn main() {
let a = Rc::new(Node { name: String::from("a"), other: RefCell::new(None) });
let b = Rc::new(Node { name: String::from("b"), other: RefCell::new(None) });
*a.other.borrow_mut() = Some(Rc::clone(&b));
*b.other.borrow_mut() = Some(Rc::clone(&a));
println!("a has {} owners", Rc::strong_count(&a));
drop(a);
drop(b);
println!("names dropped");
}a has 2 owners
names droppedWeak: a link back that doesn't keep anything alive
The usual fix is to make one direction weak. A parent owns its children with Rc; a child
points back at its parent with Weak, which doesn't count as an owner. Python has the same tool in
weakref. To use a Weak, call .upgrade(): it gives Some while the value
is alive and None once it has been freed, just like calling a Python weak reference.
import weakref
class Parent:
def __init__(self, name):
self.name = name
self.children = []
def __del__(self):
print(f"free {self.name}")
class Child:
def __init__(self, name, parent):
self.name = name
self.parent = weakref.ref(parent)
def __del__(self):
print(f"free {self.name}")
mom = Parent("mom")
kid = Child("kid", mom)
mom.children.append(kid)
print(f"{kid.name}'s parent is {kid.parent().name}")
del kid
del mom
print("end")use std::cell::RefCell;
use std::rc::{Rc, Weak};
struct Parent {
name: String,
children: RefCell<Vec<Rc<Child>>>,
}
struct Child {
name: String,
parent: Weak<Parent>,
}
impl Drop for Parent {
fn drop(&mut self) {
println!("free {}", self.name);
}
}
impl Drop for Child {
fn drop(&mut self) {
println!("free {}", self.name);
}
}
fn main() {
let mom = Rc::new(Parent { name: String::from("mom"), children: RefCell::new(Vec::new()) });
let kid = Rc::new(Child { name: String::from("kid"), parent: Rc::downgrade(&mom) });
mom.children.borrow_mut().push(Rc::clone(&kid));
println!("{}'s parent is {}", kid.name, kid.parent.upgrade().unwrap().name);
drop(kid);
drop(mom);
println!("end");
}kid's parent is mom
free mom
free kid
endWhere the Python analogy breaks
- Every Python object is shared and reference counted, and you never ask for it. In Rust it is opt-in, per value, with
Rc, and most values keep exactly one owner. Rcis read-only. Python lets anyone holding an object change it. In Rust, changing shared data needsRc<RefCell<T>>.RefCellmoves the borrow check to run time. Twoborrow_mut()at the same time compile fine and then panic. Python never stops you, so two parts of a program can quietly change the same object.- Cycles leak. Python's
gcfinds objects that point at each other and frees them.Rchas no cycle collector, so useWeakfor links back, such as child → parent, the way you'd useweakref. Rcis for one thread only, and the compiler enforces it. Across threads you useArc(an atomic reference count), withMutexinstead ofRefCell.
Cheat card
| Python | Rust |
|---|---|
a = b # any object, shared | let a = Rc::clone(&b); |
a is b | Rc::ptr_eq(&a, &b) |
sys.getrefcount(x) | Rc::strong_count(&x) |
obj.field = 1 # through any name | obj.borrow_mut().field = 1; // Rc<RefCell<T>> |
print(obj.field) | println!("{}", obj.borrow().field); |
weakref.ref(obj) | Rc::downgrade(&obj) // a Weak<T> |
ref() # the object or None | weak.upgrade() // Some(rc) or None |
gc.collect() # frees cycles | // no cycle collector: break cycles with Weak |
# threads share objects | Arc<Mutex<T>> // Rc<RefCell<T>> for threads |
← Memory without a garbage collector Threads without a GIL →