0
0
Rustprogramming~10 mins

Ownership with smart pointers in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Ownership with smart pointers
Create smart pointer
Pointer owns data
Use data via pointer
Pointer goes out of scope
Data is dropped automatically
This flow shows how a smart pointer owns data, allows safe use, and cleans up automatically when it goes out of scope.
Execution Sample
Rust
fn main() {
    let s = Box::new(String::from("hello"));
    println!("{}", s);
}
Creates a Box smart pointer owning a String, prints it, then automatically frees memory when done.
Execution Table
StepActionPointer StateData StateOutput
1Create Box smart pointer ss owns String("hello")String("hello") allocated on heap
2Print value via ss owns String("hello")String("hello") accessiblehello
3End of scope, s droppeds dropped, no longer validString("hello") memory freed
💡 Pointer s goes out of scope, Rust automatically frees the owned data.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3 (end)
suninitializedBox owning String("hello")Box owning String("hello")dropped (no longer valid)
String dataunallocatedallocated on heapaccessible via sfreed
Key Moments - 3 Insights
Why does the String data get freed automatically without calling drop()?
Because the Box smart pointer owns the data and Rust’s ownership rules automatically call drop when s goes out of scope (see execution_table step 3).
Can we use s after it is dropped at the end of main?
No, after step 3 in execution_table, s is dropped and no longer valid, so using it would cause a compile error.
Why is the String data stored on the heap and not the stack?
Box allocates data on the heap to allow ownership and dynamic size, while the pointer s itself is on the stack (see execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the output when printing s?
Ahello
BBox pointer address
CString data is empty
DCompilation error
💡 Hint
Check the Output column at step 2 in execution_table.
At which step does the Box pointer s get dropped and free the String data?
AStep 1
BStep 3
CStep 2
DNever dropped
💡 Hint
Look at the Action and Pointer State columns in execution_table step 3.
If we tried to use s after step 3, what would happen?
AIt would print the String again
BProgram crashes at runtime
CRust compiler error for use after drop
Ds would be reinitialized automatically
💡 Hint
Refer to variable_tracker showing s is dropped after step 3.
Concept Snapshot
Ownership with smart pointers in Rust:
- Smart pointers like Box own heap data
- Data is accessed safely via the pointer
- When pointer goes out of scope, data is freed automatically
- Prevents manual memory errors
- Enables safe, automatic cleanup
Full Transcript
This example shows how Rust uses smart pointers like Box to own data on the heap. When we create s as Box::new(String::from("hello")), s owns the String data. We can use s to access and print the string safely. When s goes out of scope at the end of main, Rust automatically calls drop to free the heap memory. This automatic cleanup prevents memory leaks and errors. Trying to use s after it is dropped would cause a compile-time error. The variable tracker shows s owning the data after creation, and being dropped at the end. The execution table traces each step: creation, usage, and cleanup. This flow helps beginners understand ownership and automatic memory management with smart pointers in Rust.