Bird
Raised Fist0
Rustprogramming~10 mins

Variable shadowing in Rust - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to shadow the variable x with a new value.

Rust
let x = 5;
let x = [1];
println!("{}", x);
Drag options to blanks, or click blank then click option'
Ax + 1
Bx - 1
Cx * 2
Dx / 2
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the same value instead of a new one.
Trying to modify x without shadowing.
2fill in blank
medium

Complete the code to shadow y with a string value.

Rust
let y = 10;
let y = [1];
println!("{}", y);
Drag options to blanks, or click blank then click option'
A20
B"ten"
Cy + 5
Dtrue
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to assign a number instead of a string.
Not using quotes for the string.
3fill in blank
hard

Fix the error by shadowing z with a new value inside the block.

Rust
let z = 3;
{
    let [1] = z + 2;
    println!("{}", z);
}
println!("{}", z);
Drag options to blanks, or click blank then click option'
Az_new
Bx
Cy
Dz
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a different variable name instead of shadowing.
Trying to modify z without shadowing.
4fill in blank
hard

Fill both blanks to shadow a with a new value and then shadow it again with a string.

Rust
let a = 1;
let a = [1];
let a = [2];
println!("{}", a);
Drag options to blanks, or click blank then click option'
Aa + 1
B"hello"
C"world"
Da * 2
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to shadow with a number then a number again.
Not using quotes for the string shadow.
5fill in blank
hard

Fill all three blanks to shadow val by doubling, then converting to string, then appending an exclamation mark.

Rust
let val = 4;
let val = val [1] 2;
let val = val.to_string();
let val = val [2] [3];
println!("{}", val);
Drag options to blanks, or click blank then click option'
A*
B+
C"!"
D-
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using - instead of + for string concatenation.
Trying to multiply strings.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Reuse the same variable name to store a new value or type -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    let x = 5; let x = x + 1; println!("{}", x); -> Option A
  4. 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

  1. 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.
  2. Step 2: Understand the final print output

    The last x is a String "Value: 20", so printing it outputs exactly that.
  3. Final Answer:

    Value: 20 -> Option D
  4. 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

  1. Step 1: Analyze shadowing and mutability

    First x is immutable 5, then shadowed to 6 (immutable), then shadowed to mutable 12.
  2. Step 2: Check assignment and print

    Since the last x is mutable, assigning x = 10; is valid. Printing outputs 10.
  3. Final Answer:

    No error; code runs and prints 10 -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Inner shadowing does not affect outer variable [OK]
Hint: Inner scope shadowing doesn't change outer variable [OK]
Common Mistakes:
  • Assuming inner shadowing changes outer variable
  • Confusing printed output with variable value
  • Expecting type change to persist outside inner scope
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard