๐ Scenario: Imagine you are organizing a small event and need to keep track of the number of guests in different rooms. You want to understand how variables work inside and outside of blocks in Rust.
๐ฏ Goal: You will create variables inside and outside blocks to see how their scope affects their accessibility and values.
๐ What You'll Learn
Create a variable called guests with the value 10 outside any block.
Create a block and inside it create a variable called guests with the value 5.
Print the value of guests inside the block.
Print the value of guests outside the block.
๐ก Why This Matters
๐ Real World
Understanding variable scope helps you write clear and bug-free programs by controlling where data can be accessed or changed.
๐ผ Career
Many programming jobs require knowledge of variable scope to manage memory and avoid errors in complex code.
Progress0 / 4 steps
1
Create the outer variable guests
Create a variable called guests and set it to 10 outside any block.
Rust
Hint
Use let guests = 10; to create the variable.
2
Create an inner block with a new guests variable
Inside the main function, create a block using curly braces { }. Inside this block, create a new variable called guests and set it to 5.
Rust
Hint
Use curly braces to create a block and inside it write let guests = 5;.
3
Print the inner guests value inside the block
Inside the inner block, add a println! statement to print the value of guests.
Rust
Hint
Use println!("Guests inside block: {}", guests); to print the value.
4
Print the outer guests value outside the block
After the inner block, add a println! statement to print the value of the outer guests variable.
Rust
Hint
Use println!("Guests outside block: {}", guests); after the block to print the outer variable.
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);
}