0
0
Rustprogramming~10 mins

Scope of variables in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Scope of variables
Start of program
Declare variable in main scope
Enter inner block
Declare variable in inner block
Use variables
Exit inner block
Try to use inner block variable (error)
End of program
This flow shows how variables declared inside blocks are only usable within those blocks, while variables declared outside are accessible inside.
Execution Sample
Rust
fn main() {
    let x = 5;
    {
        let y = 10;
        println!("x = {}, y = {}", x, y);
    }
    println!("x = {}", x);
    // println!("y = {}", y); // Error: y not in scope
}
This Rust code shows variable x declared in main scope and y declared inside an inner block; y is not accessible outside.
Execution Table
StepActionVariables in ScopeOutputNotes
1Declare x = 5 in main scopex=5x is accessible everywhere in main after this
2Enter inner blockx=5Inner block starts, x still accessible
3Declare y = 10 in inner blockx=5, y=10y is only accessible inside this block
4Print x and yx=5, y=10x = 5, y = 10Both variables accessible here
5Exit inner blockx=5y goes out of scope, no longer accessible
6Print xx=5x = 5x still accessible
7Attempt to print y (commented out)x=5Error if uncommented: y not in scope
💡 Program ends after main function finishes; inner block variables are dropped after block ends.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
xundefined5555
yundefinedundefined10undefinedundefined
Key Moments - 2 Insights
Why can't we use variable y outside the inner block?
Because y is declared inside the inner block (see step 3 and 5 in execution_table), it only exists within that block's scope and is dropped after the block ends.
Is variable x accessible inside the inner block?
Yes, x is declared in the main scope before the inner block (step 1), so it is accessible inside the inner block (step 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what variables are in scope?
AOnly x
Bx and y
COnly y
DNeither x nor y
💡 Hint
Check the 'Variables in Scope' column at step 4 in execution_table.
At which step does variable y go out of scope?
AStep 5
BStep 3
CStep 6
DStep 7
💡 Hint
Look at the 'Variables in Scope' column before and after step 5 in execution_table.
If we declare y outside the inner block, how would the variable_tracker change?
Ay would only appear after step 3
By would be undefined throughout
Cy would have a value starting from step 1 and remain until final
Dy would disappear after step 5
💡 Hint
Variable declared outside block stays in scope throughout; see variable_tracker for x.
Concept Snapshot
Scope of variables in Rust:
- Variables declared inside a block {} are only accessible inside that block.
- Variables declared outside are accessible inside inner blocks.
- After the block ends, inner variables go out of scope and cannot be used.
- Trying to use out-of-scope variables causes compile errors.
Full Transcript
This visual execution shows how Rust handles variable scope. Variable x is declared in the main function and is accessible everywhere inside main. Variable y is declared inside an inner block and is only accessible within that block. When the block ends, y goes out of scope and cannot be used anymore. The execution table traces each step, showing which variables are in scope and what output is produced. Key moments clarify why y is not accessible outside the block and confirm that x is accessible inside. The quiz tests understanding of variable scope changes during execution.