Curriculum Production Rust Interior mutability
Interior mutability
Sometimes Rust permits mutation through a shared reference by moving borrow checks to runtime. This theme teaches Cell, RefCell, and the Rc<RefCell<T>> pattern as tools with costs, not escape hatches. Planned exercises: 1. choose RefCell for runtime-checked mutation. 2. recognise borrow_mut. 3. choose Cell for small copy values. 4. fill RefCell::new. 5. fill a scoped mutable borrow. 6. write a counter using Cell. 7. write a shared mutable graph edge with Rc<RefCell<_>>. 8. translate TS shared mutation deliberately. 9. fix a runtime borrow panic by shortening a borrow.
Exercises
9 ready
01
pick one
A struct field needs to mutate through a SHARED reference
02
pick one
Given let cell = RefCell::new(0_i32);, pick the call that
03
pick one
A struct holds a single u32 counter that needs to update
04
fill blanks
Fill the associated function that wraps an i32 value in a
05
type one line
Inside an inner block, write the line that mutably borrows
06
write a program
Define struct Counter { count: Cell<u32> } with an impl
07
write a program
Use the Rc<RefCell<T>> pattern to share a mutable i32
08
write a program
Translate the TS "function mutates a passed-in object"
09
write a program
This program PANICS at runtime — RefCell enforces the