Step Into Rust · Lesson 11 · Sharing & Concurrency
Box, Rc and RefCell
Recursive types on the heap, shared ownership with reference counts, mutation behind shared references, and Weak to break cycles.
- ~35 minreading and coding
- 4missions
- membersaccess
- Rustlanguage
Part of Step Into Rust. The full lesson, its missions and the real compiler and debugger are in the course. Try a free lesson first to see how it works.
A type that contains itself directly would be infinitely large. Putting the inner value behind a Box makes it one pointer:
In this lesson
- Box: recursion needs a fixed size
- Rc: more than one owner
- RefCell: borrow rules checked at run time
- Weak: references that do not own
Missions
- A tree of boxes
- Count the owners
- One log, two writers
- A parent you do not own