Bird
Raised Fist0
Rustprogramming~20 mins

Scope of variables in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code with variable shadowing?

Consider the following Rust code snippet. What will it print?

Rust
fn main() {
    let x = 5;
    {
        let x = x + 1;
        println!("{}", x);
    }
    println!("{}", x);
}
A6\n5
B6\n6
C5\n6
D5\n5
Attempts:
2 left
๐Ÿ’ก Hint

Remember that inner let shadows the outer variable.

โ“ Predict Output
intermediate
2:00remaining
What error does this Rust code produce due to variable scope?

What error will this Rust code produce when compiled?

Rust
fn main() {
    {
        let y = 10;
    }
    println!("{}", y);
}
Aerror: cannot find value `y` in this scope
Berror: use of moved value: `y`
Cerror: expected `;` after expression
DNo error, prints 10
Attempts:
2 left
๐Ÿ’ก Hint

Variables declared inside a block are not accessible outside it.

๐Ÿ”ง Debug
advanced
2:30remaining
Why does this Rust code fail to compile due to variable borrowing and scope?

Examine the code below. Why does it fail to compile?

Rust
fn main() {
    let mut s = String::from("hello");
    let r1 = &s;
    let r2 = &s;
    let r3 = &mut s;
    println!("{} {} {}", r1, r2, r3);
}
ACannot borrow `s` as immutable because it is already borrowed as mutable
BNo error, prints: hello hello hello
CCannot print multiple references in one println!
DCannot borrow `s` as mutable because it is also borrowed as immutable
Attempts:
2 left
๐Ÿ’ก Hint

Rust enforces rules about mutable and immutable references coexisting.

โ“ Predict Output
advanced
2:00remaining
What is the output of this Rust code with nested scopes and variable shadowing?

What will this Rust program print?

Rust
fn main() {
    let a = 1;
    {
        let a = 2;
        {
            let a = 3;
            println!("{}", a);
        }
        println!("{}", a);
    }
    println!("{}", a);
}
A3\n3\n3
B1\n2\n3
C3\n2\n1
D1\n1\n1
Attempts:
2 left
๐Ÿ’ก Hint

Each inner block shadows the variable from the outer block.

โ“ Predict Output
expert
3:00remaining
What is the value of `x` after this Rust code runs with shadowing and mutation?

After running this Rust code, what is the final value of x printed?

Rust
fn main() {
    let mut x = 5;
    {
        let x = &mut x;
        *x += 1;
    }
    println!("{}", x);
}
ACompilation error due to borrowing
B6
C7
D5
Attempts:
2 left
๐Ÿ’ก Hint

Understand how mutable references and shadowing work together.

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

  1. Step 1: Understand the meaning of scope

    Scope means the part of the code where a variable is valid and can be used.
  2. Step 2: Identify what scope controls

    Scope controls access to the variable, not its type, value, or memory size.
  3. Final Answer:

    Where the variable can be accessed or used -> Option A
  4. 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

  1. Step 1: Recall Rust variable declaration syntax

    Rust uses let keyword to declare variables.
  2. Step 2: Check each option

    Only let x = 5; is valid Rust syntax; others are from other languages or invalid.
  3. Final Answer:

    let x = 5; -> Option B
  4. 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);
}
medium
A. 10\n10
B. 20\n20
C. 10\n20
D. 20\n10

Solution

  1. Step 1: Identify variable shadowing inside inner block

    Inside the inner block, x is redefined as 20, shadowing outer x.
  2. Step 2: Trace the print statements

    First print shows inner x = 20; second print shows outer x = 10.
  3. Final Answer:

    20 10 -> Option D
  4. Quick Check:

    Inner shadow = 20, outer original = 10 [OK]
Hint: Inner block shadows outer variable [OK]
Common Mistakes:
  • Ignoring shadowing and printing outer value twice
  • Assuming inner variable changes outer variable
  • Confusing print order
4. Identify the error in this Rust code snippet:
fn main() {
    {
        let y = 5;
    }
    println!("{}", y);
}
medium
A. Variable y is used outside its scope
B. Missing semicolon after let y = 5
C. println! macro is misspelled
D. No error, code runs fine

Solution

  1. Step 1: Check where y is declared and used

    Variable y is declared inside inner block and goes out of scope after block ends.
  2. Step 2: Analyze usage of y outside block

    Using y in println! outside block causes error because y is not visible there.
  3. Final Answer:

    Variable y is used outside its scope -> Option A
  4. Quick Check:

    Use inside scope only [OK]
Hint: Variables vanish outside their block scope [OK]
Common Mistakes:
  • Thinking semicolon is missing
  • Assuming println! is wrong
  • Ignoring variable scope rules
5. Consider this Rust code:
fn main() {
    let mut count = 0;
    for i in 0..3 {
        let count = count + i;
        println!("{}", count);
    }
    println!("Final count: {}", count);
}
What will be the output and why?
hard
A. 0\n1\n2\nFinal count: 6
B. 0\n1\n2\nFinal count: 2
C. 0\n1\n2\nFinal count: 0
D. Compilation error due to variable shadowing

Solution

  1. Step 1: Understand variable shadowing inside the loop

    Inside the loop, count is shadowed by a new variable count equal to outer count plus i. Outer count remains unchanged.
  2. Step 2: Trace loop iterations and prints

    Iterations: i=0 -> inner count=0, print 0; i=1 -> inner count=1, print 1; i=2 -> inner count=2, print 2. Outer count stays 0.
  3. Step 3: Final print shows outer count

    After loop, outer count is still 0, so prints "Final count: 0".
  4. Final Answer:

    0 1 2 Final count: 0 -> Option C
  5. Quick Check:

    Shadowing hides outer count, outer unchanged [OK]
Hint: Shadowing inside loop doesn't change outer variable [OK]
Common Mistakes:
  • Assuming outer count changes inside loop
  • Confusing shadowing with mutation
  • Expecting sum accumulation in outer count
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