0
0
Rustprogramming~10 mins

RefCell overview in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - RefCell overview
Create RefCell with value
Borrow immutably?
NoBorrow mutably?
Return Ref<T>
Use value inside RefCell
Borrow ends, Ref/RefMut dropped
RefCell ready for next borrow
RefCell allows mutable borrowing checked at runtime, enabling multiple immutable or one mutable borrow inside single ownership.
Execution Sample
Rust
use std::cell::RefCell;

fn main() {
    let data = RefCell::new(5);
    let r1 = data.borrow();
    let r2 = data.borrow();
    println!("{} {}", *r1, *r2);
}
Creates a RefCell holding 5, borrows it immutably twice, then prints both values.
Execution Table
StepActionBorrow TypeBorrow Count (immutable, mutable)ResultOutput
1Create RefCell with value 5None(0, 0)RefCell created
2Call borrow()Immutable(1, 0)Returns Ref to 5
3Call borrow() againImmutable(2, 0)Returns another Ref to 5
4Print valuesImmutable(2, 0)Reads values 5 and 55 5
5Refs go out of scopeNone(0, 0)Borrows released
💡 All immutable borrows ended, RefCell ready for new borrows
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5
dataRefCell(5)RefCell(5)RefCell(5)RefCell(5)
r1NoneRef(5)Ref(5)None
r2NoneNoneRef(5)None
Key Moments - 3 Insights
Why can we call borrow() twice without error?
Because RefCell allows multiple immutable borrows at the same time, as shown in steps 2 and 3 where borrow count is (2, 0).
What happens if we try to borrow mutably while immutable borrows exist?
RefCell will panic at runtime because mutable borrow requires no other borrows, unlike the allowed multiple immutable borrows.
Why do r1 and r2 become None after step 5?
Because the Ref objects go out of scope, releasing the immutable borrows and resetting borrow counts to zero.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the borrow count after step 3?
A(1, 1)
B(2, 0)
C(0, 1)
D(0, 0)
💡 Hint
Check the 'Borrow Count' column at step 3 in the execution_table.
At which step do the immutable borrows end?
AStep 5
BStep 4
CStep 3
DStep 2
💡 Hint
Look for when borrow count resets to (0, 0) in the execution_table.
If we tried a mutable borrow at step 3, what would happen?
AIt would succeed and borrow count becomes (2,1)
BIt would silently ignore the mutable borrow
CIt would panic at runtime
DIt would convert immutable borrows to mutable
💡 Hint
Recall RefCell rules: mutable borrow requires no other borrows; see key_moments about mutable borrow conflicts.
Concept Snapshot
RefCell<T> allows interior mutability checked at runtime.
Use borrow() for immutable access (multiple allowed).
Use borrow_mut() for mutable access (only one allowed).
Violations cause runtime panic.
Useful when you need mutability inside immutable structures.
Full Transcript
This visual trace shows how RefCell in Rust manages borrowing at runtime. We start by creating a RefCell holding the value 5. Then we borrow it immutably twice, increasing the immutable borrow count to 2. Both borrows return Ref objects that allow read access. We print the values, both 5, then the borrows end as the Ref objects go out of scope, resetting borrow counts. RefCell enforces borrowing rules at runtime: multiple immutable borrows allowed, but mutable borrow requires exclusive access. Attempting mutable borrow while immutable borrows exist causes panic. This helps safely mutate data inside otherwise immutable contexts.