Variable shadowing in Rust means declaring a new variable with the same name as a previous variable. The new variable temporarily hides the old one within its scope.
Click to reveal answer
intermediate
How does shadowing differ from mutability in Rust?
Shadowing lets you reuse a variable name by creating a new variable, even if the original was immutable. Mutability changes the value of the same variable without creating a new one.
Click to reveal answer
intermediate
What happens if you shadow a variable with a different type?
Rust allows shadowing with a different type. The new variable can have a different type than the original, enabling transformations like converting a string to a number.
Click to reveal answer
beginner
Consider this Rust code:<br><pre>let x = 5;<br>let x = x + 1;<br>let x = x * 2;<br>println!("{}", x);</pre><br>What is the output?
The output is 12. Explanation: - First x is 5. - Then shadowed x is 6 (5 + 1). - Then shadowed x is 12 (6 * 2). - Finally, 12 is printed.
Click to reveal answer
beginner
Why is variable shadowing useful in Rust?
Shadowing helps keep code clean by reusing variable names for updated values without needing new names. It also allows changing types safely and avoids mutability when not needed.
Click to reveal answer
What does variable shadowing allow you to do in Rust?
ADeclare a new variable with the same name as an existing one
BChange the value of a mutable variable
CDelete a variable from memory
DCreate a global variable
✗ Incorrect
Variable shadowing lets you declare a new variable with the same name, hiding the old one temporarily.
Can you shadow a variable with a different type in Rust?
AOnly if the variable is mutable
BYes, Rust allows changing the type when shadowing
CNo, the type must stay the same
DOnly inside functions
✗ Incorrect
Rust permits shadowing with a different type, enabling type transformations.
What will this Rust code print?
let a = 3; let a = a * 4; println!("{}", a);
A3
B7
C12
DError
✗ Incorrect
The variable a is shadowed and updated to 12 (3 * 4), which is printed.
Which of these is NOT true about shadowing in Rust?
AShadowing hides the previous variable temporarily
BShadowing can change the variable's type
CShadowing creates a new variable
DShadowing requires the variable to be mutable
✗ Incorrect
Shadowing does NOT require mutability; it works even with immutable variables.
Why might you prefer shadowing over mutability?
ATo keep variables immutable and avoid bugs
BTo make variables global
CTo delete variables
DTo speed up the program
✗ Incorrect
Shadowing lets you update values while keeping variables immutable, which can reduce bugs.
Explain variable shadowing in Rust and give a simple example.
Think about declaring the same variable name twice.
You got /3 concepts.
Describe the benefits of using variable shadowing instead of mutability in Rust.
Why might you want to avoid mutable variables?
You got /3 concepts.
Practice
(1/5)
1. What does variable shadowing in Rust allow you to do?
easy
A. Prevent variables from being changed after initialization
B. Create a global variable accessible everywhere
C. Reuse the same variable name to store a new value or type
D. Automatically convert variable types without explicit casting
Solution
Step 1: Understand variable shadowing concept
Variable shadowing means declaring a new variable with the same name as an existing one, which replaces the old variable in the current scope.
Step 2: Identify what shadowing allows
This lets you reuse the same name to update the value or even change the type without needing a new variable name.
Final Answer:
Reuse the same variable name to store a new value or type -> Option C
Quick Check:
Shadowing = reuse variable name [OK]
Hint: Shadowing means reusing variable names for new values [OK]
Common Mistakes:
Thinking shadowing creates global variables
Confusing shadowing with immutability
Assuming shadowing changes variable type automatically
2. Which of the following Rust code snippets correctly demonstrates variable shadowing?
easy
A. let x = 5; let x = x + 1; println!("{}", x);
B. let x = 5; x = x + 1; println!("{}", x);
C. let mut x = 5; x = x + 1; println!("{}", x);
D. let x = 5; mut x = 6; println!("{}", x);
Solution
Step 1: Check for correct shadowing syntax
Shadowing uses let again with the same variable name to create a new variable that hides the old one.
Step 2: Analyze each option
let x = 5; let x = x + 1; println!("{}", x);: let x = 5; let x = x + 1; println!("{}", x); valid shadowing using previous value. let x = 5; x = x + 1; println!("{}", x);: let x = 5; x = x + 1; ... reassignment to immutable, compile error. let mut x = 5; x = x + 1; println!("{}", x);: let mut x = 5; x = x + 1; ... valid mutation but no shadowing (missing second let). let x = 5; mut x = 6; println!("{}", x);: let x = 5; mut x = 6; ... invalid syntax, mut requires let.
Final Answer:
let x = 5; let x = x + 1; println!("{}", x); -> Option A
Quick Check:
Shadowing requires let again [OK]
Hint: Shadowing always uses 'let' again with the same name [OK]
Common Mistakes:
Trying to reassign immutable variables without 'mut'
Confusing shadowing with mutable reassignment
Using 'mut' incorrectly with shadowing
3. What will be the output of this Rust code?
let x = 10;
let x = x * 2;
let x = "Value: ".to_string() + &x.to_string();
println!("{}", x);
medium
A. 20
B. Value: x
C. Compilation error
D. Value: 20
Solution
Step 1: Follow variable shadowing and type changes
First, x is 10 (integer). Then shadowed to 20 (integer). Then shadowed again to a String "Value: 20" by concatenating strings.
Step 2: Understand the final print output
The last x is a String "Value: 20", so printing it outputs exactly that.
Final Answer:
Value: 20 -> Option D
Quick Check:
Shadowing can change type; final value is string [OK]
Hint: Shadowing can change variable type and value [OK]
Common Mistakes:
Assuming variable type cannot change with shadowing
Expecting numeric output instead of string
Confusing variable name with literal output
4. Identify the error in this Rust code using variable shadowing:
let x = 5;
let x = x + 1;
let mut x = x * 2;
x = 10;
println!("{}", x);
medium
A. Cannot shadow a variable with 'mut' after immutable shadowing
B. No error; code runs and prints 10
C. Shadowing requires the same type, but types differ here
D. Cannot assign to 'x' because it is not mutable
Solution
Step 1: Analyze shadowing and mutability
First x is immutable 5, then shadowed to 6 (immutable), then shadowed to mutable 12.
Step 2: Check assignment and print
Since the last x is mutable, assigning x = 10; is valid. Printing outputs 10.
Final Answer:
No error; code runs and prints 10 -> Option B
Quick Check:
Shadowing can add mutability; assignment allowed [OK]
Hint: Shadowing can change mutability; last variable controls it [OK]
Common Mistakes:
Thinking mutability cannot be added by shadowing
Confusing shadowing with reassignment rules
Assuming type must stay the same when shadowing
5. Given this Rust code, what is the final value of result?
let x = 3;
let x = x + 2;
{
let x = x * 2;
let x = "Number: ".to_string() + &x.to_string();
println!("{}", x);
}
let result = x + 1;
println!("{}", result);
hard
A. 6
B. 7
C. Number: 10
D. Compilation error due to type mismatch
Solution
Step 1: Track shadowing in outer and inner scopes
Outside the block, x is first 3, then shadowed to 5. Inside the block, x shadows to 10 (5*2), then to a String "Number: 10". This inner shadowing does not affect outer x.
Step 2: Calculate final result outside the block
Outside, x remains 5, so result = x + 1 = 6. The print inside the block shows "Number: 10" but does not change outer x.
Final Answer:
6 -> Option A
Quick Check:
Inner shadowing does not affect outer variable [OK]