Flash cards
Review the key moves
What is the main idea behind Rust Scope?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
fn ___() {Put the learning moves in the order that makes the concept easiest to apply.
Scope
Now that you understand how functions work, it is important to learn how variables act inside and outside of functions.
Scope refers to where a variable is allowed to be used.
A variable only lives inside the block where it was created. A block is anything inside curly braces { } .
Variable Inside a Function
A variable created inside a function only exists inside that function:
Example
fn myFunction() {
let message = "Hello!";
println!("{}", message); // You can access the message
variable here
}
myFunction();
println!("{}", message); // Error - you cannot access the message variable
outside of the functionNote
The variable message only exists inside the function. Trying to use it outside the function will cause an error.
Variable Inside a Block
You can also create blocks inside other code, like in if statements or loops. Variables created in these blocks are only valid inside them.
Example
let score = 80;
if score > 50 {
let result = "Pass";
println!("Result: {}", result);
}
println!("Result: {}", result);
// Error: result is out of scope hereVariables in the Same Scope
In Rust, you can declare a new variable with the same name in the same scope using let . This is called shadowing :
Example
let x = 5;
let x = 10;
println!("x is: {}", x); // prints 10The second x replaces the first one. The value 5 is no longer accessible after the second declaration.
This is different from languages that disallow reusing variable names. In Rust, it's a feature used to transform or update values safely.
You can also reuse a variable name inside a new block:
Example
let x = 5; {
let x = 10;
println!("Inside block: {}", x);
}
println!("Outside block: {}", x);Here, the two x variables are in different scopes. The inner x only exists inside the block. Outside the block, the original value remains.
Why Scope Matters
Understanding scope helps you
- Know where a variable can be used
- Prevent naming conflicts
- Avoid errors when working with functions, loops, and conditionals