Bird
Raised Fist0
Rustprogramming~20 mins

For loop in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust For Loop 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 for loop?
Consider the following Rust code. What will it print?
Rust
fn main() {
    let mut sum = 0;
    for i in 1..5 {
        sum += i;
    }
    println!("{}", sum);
}
AError: cannot add i to sum
B15
C14
D10
Attempts:
2 left
๐Ÿ’ก Hint
Remember that 1..5 in Rust means 1 up to but not including 5.
๐Ÿง  Conceptual
intermediate
1:30remaining
How many times does this Rust for loop run?
How many iterations will this loop execute? for x in (0..10).step_by(3) { println!("{}", x); }
A4
BError: step_by requires usize
C10
D3
Attempts:
2 left
๐Ÿ’ก Hint
Count numbers from 0 to 9 stepping by 3.
โ“ Predict Output
advanced
2:30remaining
What is the output of this nested for loop?
Analyze the output of this Rust code snippet:
Rust
fn main() {
    for i in 1..3 {
        for j in 1..=i {
            print!("{}{} ", i, j);
        }
    }
}
A11 21
B11 12 21 22
C11 21 22
D12 22
Attempts:
2 left
๐Ÿ’ก Hint
Remember that 1..3 means 1 and 2, and 1..=i includes i.
๐Ÿ”ง Debug
advanced
2:00remaining
What error does this Rust for loop code produce?
Identify the error when running this code:
Rust
fn main() {
    let mut numbers = vec![1, 2, 3];
    for num in numbers.iter() {
        numbers.push(*num + 1);
    }
}
ACompile error: cannot borrow `numbers` as mutable while iterating
BSyntax error: missing semicolon
CNo error, prints numbers
DCompile error: cannot move out of `numbers`
Attempts:
2 left
๐Ÿ’ก Hint
Think about modifying a vector while looping over it.
๐Ÿš€ Application
expert
3:00remaining
What is the final value of `result` after this loop?
Given this Rust code, what is the value of `result` after the loop finishes?
Rust
fn main() {
    let mut result = 1;
    for i in (1..=4).rev() {
        if i % 2 == 0 {
            result *= i;
        }
    }
    println!("{}", result);
}
A24
B8
C4
D0
Attempts:
2 left
๐Ÿ’ก Hint
Multiply only even numbers from 4 down to 1.

Practice

(1/5)
1. What does a for loop do in Rust?
easy
A. Declares a variable
B. Defines a new function
C. Repeats actions for each item in a collection
D. Stops the program immediately

Solution

  1. Step 1: Understand the purpose of a for loop

    A for loop runs code repeatedly for each item in a list or range.
  2. Step 2: Match the description to the options

    Only Repeats actions for each item in a collection describes repeating actions over items in a collection.
  3. Final Answer:

    Repeats actions for each item in a collection -> Option C
  4. Quick Check:

    For loop = repeat over items [OK]
Hint: For loops repeat actions over collections [OK]
Common Mistakes:
  • Confusing for loop with function definition
  • Thinking for loop declares variables
  • Believing for loop stops program
2. Which of these is the correct syntax for a for loop iterating over numbers 1 to 3 in Rust?
easy
A. for i = 1 to 3 { println!("{}", i); }
B. for i in 1..=3 { println!("{}", i); }
C. for (i in 1..3) println!("{}", i);
D. for i in range(1,3) { println!("{}", i); }

Solution

  1. Step 1: Recall Rust for loop syntax

    Rust uses for variable in range { } with ranges like 1..=3 to include 3.
  2. Step 2: Check each option

    for i in 1..=3 { println!("{}", i); } uses correct Rust syntax with inclusive range. Others use invalid syntax or functions not in Rust.
  3. Final Answer:

    for i in 1..=3 { println!("{}", i); } -> Option B
  4. Quick Check:

    Rust for loop uses in and ranges [OK]
Hint: Use 'for var in start..=end' for inclusive ranges [OK]
Common Mistakes:
  • Using 'to' instead of 'in' keyword
  • Using parentheses around loop variable
  • Using Python or other language range syntax
3. What is the output of this Rust code?
let mut sum = 0;
for i in 1..4 {
    sum += i;
}
println!("{}", sum);
medium
A. 6
B. 10
C. 9
D. 7

Solution

  1. Step 1: Identify the loop range and iterations

    The loop runs for i = 1, 2, 3 because 1..4 excludes 4.
  2. Step 2: Calculate the sum of iterations

    sum = 0 + 1 + 2 + 3 = 6
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    1+2+3 = 6 [OK]
Hint: Remember range excludes the end number [OK]
Common Mistakes:
  • Including 4 in the sum
  • Starting sum at 1 instead of 0
  • Confusing inclusive and exclusive ranges
4. Find the error in this Rust for loop code:
for i in 0..5
    println!("{}", i);
}
medium
A. Variable i is not declared
B. Range syntax is incorrect
C. println! macro is used incorrectly
D. Missing curly braces around loop body

Solution

  1. Step 1: Check loop syntax

    Rust requires curly braces { } around the loop body if it has more than one statement or to be explicit.
  2. Step 2: Identify the missing brace

    The code has a closing brace '}' without an opening brace '{' before println!().
  3. Final Answer:

    Missing curly braces around loop body -> Option D
  4. Quick Check:

    For loop body needs braces [OK]
Hint: Always use braces {} for loop bodies [OK]
Common Mistakes:
  • Forgetting opening brace
  • Assuming range syntax is wrong
  • Thinking variable needs declaration outside loop
5. How can you use a for loop in Rust to print only even numbers from 2 to 10 inclusive?
hard
A. for i in 2..=10 { if i % 2 == 0 { println!("{}", i); } }
B. for i in 2..10 { if i % 2 = 0 { println!("{}", i); } }
C. for i in 2..=10 { if i / 2 == 0 { println!("{}", i); } }
D. for i in 2..=10 { if i % 2 != 0 { println!("{}", i); } }

Solution

  1. Step 1: Understand the range and condition

    We want numbers from 2 to 10 inclusive, so use 2..=10. To print even numbers, check if remainder when divided by 2 is zero.
  2. Step 2: Check each option's condition

    for i in 2..=10 { if i % 2 == 0 { println!("{}", i); } } correctly uses i % 2 == 0. for i in 2..10 { if i % 2 = 0 { println!("{}", i); } } uses assignment (=) instead of comparison (==). for i in 2..=10 { if i / 2 == 0 { println!("{}", i); } } uses division instead of modulo. for i in 2..=10 { if i % 2 != 0 { println!("{}", i); } } prints odd numbers.
  3. Final Answer:

    for i in 2..=10 { if i % 2 == 0 { println!("{}", i); } } -> Option A
  4. Quick Check:

    Use modulo == 0 for even check [OK]
Hint: Use 'if i % 2 == 0' inside for loop [OK]
Common Mistakes:
  • Using '=' instead of '==' in condition
  • Using division instead of modulo
  • Printing odd numbers by mistake
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