0
0
Rustprogramming~10 mins

Box pointer in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Box pointer
Create value on stack
Allocate heap memory
Move value to heap
Store heap address in Box pointer
Use Box pointer to access heap value
Box goes out of scope -> heap memory freed
This flow shows how a Box pointer moves a value from stack to heap, stores the heap address, and frees memory when done.
Execution Sample
Rust
fn main() {
    let b = Box::new(5);
    println!("Value in box: {}", b);
}
Creates a Box pointer holding the value 5 on the heap and prints it.
Execution Table
StepActionStackHeapBox PointerOutput
1Declare b with Box::new(5)b (Box pointer)5 (heap)Points to heap address of 5
2Print value inside bb (Box pointer)5 (heap)Points to heap address of 5Value in box: 5
3End of main, b goes out of scopeNo bHeap memory freedNo pointer
💡 b goes out of scope, Box pointer drops and heap memory is freed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3 (End)
buninitializedBox pointer to heap 5Box pointer to heap 5dropped (no longer exists)
heap memoryempty5 allocated5 allocatedfreed
Key Moments - 3 Insights
Why does the value 5 move to the heap instead of staying on the stack?
Because Box::new allocates memory on the heap and stores the value there, as shown in execution_table step 1 where heap has 5 allocated.
What happens to the heap memory when the Box pointer goes out of scope?
The heap memory is automatically freed when the Box pointer is dropped, as shown in execution_table step 3.
How do we access the value inside the Box pointer?
We use the Box pointer like a smart pointer; println! dereferences it to get the value, shown in execution_table step 2 output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what does the Box pointer 'b' point to?
AThe value 5 on the stack
BAn uninitialized memory
CThe value 5 on the heap
DThe value 0
💡 Hint
Check the 'Heap' and 'Box Pointer' columns in execution_table step 1
At which step does the heap memory get freed?
AStep 2
BStep 3
CStep 1
DNever
💡 Hint
Look at the 'Heap' column in execution_table step 3
If we print the value inside the Box pointer, what output do we get at step 2?
A"Value in box: 5"
B"Value in box: b"
C"Value in box: 0"
DNo output
💡 Hint
Check the 'Output' column in execution_table step 2
Concept Snapshot
Box pointer in Rust:
- Use Box::new(value) to allocate value on heap
- Box stores heap address on stack
- Access value via dereferencing Box
- Memory freed automatically when Box drops
- Useful for heap allocation and recursive types
Full Transcript
This example shows how Rust's Box pointer works. First, a value 5 is created on the heap using Box::new. The Box pointer 'b' stores the address of this heap memory on the stack. When we print 'b', Rust dereferences the Box to get the value 5. Finally, when 'b' goes out of scope, Rust automatically frees the heap memory. This process helps manage heap allocation safely and efficiently.