Scope tells us where a variable can be used in the code. It helps keep things organized and avoids mistakes.
When you want to use a variable only inside a small part of your program.
When you want to avoid changing a variable by mistake in other parts of the code.
When you want to reuse the same variable name in different parts without conflict.
When you want to understand where a variable is created and where it stops existing.
Syntax
Rust
fn main() {
let x = 5; // x is valid from here
{
let y = 10; // y is valid only inside this blockprintln!("x = {}, y = {}", x, y);
} // y is no longer valid hereprintln!("x = {}", x);
// println!("y = {}", y); // This would cause an error
}
Variables declared inside curly braces {} are only usable inside those braces.
Variables declared outside are usable inside inner blocks too.
Examples
Variable b is only inside the inner block. Outside, it is not known.
Rust
fn main() {
let a = 1;
{
let b = 2;
println!("a = {}, b = {}", a, b);
}
// println!("b = {}", b); // Error: b not in scope here
}
Variable x is declared in main and can be used anywhere inside main.
Rust
fn main() {
let x = 10;
println!("x = {}", x);
}
You can declare a new variable with the same name inside a block. It hides the outer one temporarily.
Rust
fn main() {
let x = 5;
{
let x = 10; // shadows outer xprintln!("inner x = {}", x);
}
println!("outer x = {}", x);
}
Sample Program
This program shows how a variable inside a block can hide the same variable outside. The outer variable keeps its value after the block.
Rust
fn main() {
let number = 7;
println!("Outside block: number = {}", number);
{
let number = 42; // shadows outer numberprintln!("Inside block: number = {}", number);
}
println!("After block: number = {}", number);
}
OutputSuccess
Important Notes
Shadowing lets you reuse variable names safely inside smaller scopes.
Trying to use a variable outside its scope causes a compile error.
Summary
Scope defines where a variable can be used.
Variables inside blocks are only valid inside those blocks.
Shadowing allows temporary replacement of a variable inside a smaller scope.
Practice
(1/5)
1. What does the scope of a variable in Rust determine?
easy
A. Where the variable can be accessed or used
B. The type of the variable
C. The value stored in the variable
D. The memory size of the variable
Solution
Step 1: Understand the meaning of scope
Scope means the part of the code where a variable is valid and can be used.
Step 2: Identify what scope controls
Scope controls access to the variable, not its type, value, or memory size.
Final Answer:
Where the variable can be accessed or used -> Option A
Quick Check:
Scope = access area [OK]
Hint: Scope means where you can use the variable [OK]
Common Mistakes:
Confusing scope with variable type
Thinking scope controls variable value
Mixing scope with memory size
2. Which of the following is the correct way to declare a variable inside a block in Rust?
easy
A. var x = 5;
B. let x = 5;
C. int x = 5;
D. declare x = 5;
Solution
Step 1: Recall Rust variable declaration syntax
Rust uses let keyword to declare variables.
Step 2: Check each option
Only let x = 5; is valid Rust syntax; others are from other languages or invalid.
Final Answer:
let x = 5; -> Option B
Quick Check:
Rust variables start with let [OK]
Hint: Rust variables start with let keyword [OK]
Common Mistakes:
Using var instead of let
Using type declarations like int
Using declare keyword
3. What will be the output of this Rust code?
fn main() {
let x = 10;
{
let x = 20;
println!("{}", x);
}
println!("{}", x);
}