Bird
Raised Fist0
Rustprogramming~20 mins

Loop execution flow in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of nested loops with break
What is the output of this Rust code snippet?
Rust
fn main() {
    let mut count = 0;
    for i in 1..=3 {
        for j in 1..=3 {
            if i * j > 4 {
                break;
            }
            count += 1;
        }
    }
    println!("{}", count);
}
A9
B5
C7
D6
Attempts:
2 left
๐Ÿ’ก Hint
Count how many times the inner loop runs before break triggers.
โ“ Predict Output
intermediate
2:00remaining
Loop with continue and variable update
What will be printed by this Rust program?
Rust
fn main() {
    let mut sum = 0;
    for x in 1..=5 {
        if x % 2 == 0 {
            continue;
        }
        sum += x;
    }
    println!("{}", sum);
}
A6
B9
C15
D10
Attempts:
2 left
๐Ÿ’ก Hint
Sum only odd numbers from 1 to 5.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in this loop
What error will this Rust code produce when compiled?
Rust
fn main() {
    let mut i = 0;
    loop {
        if i == 5 {
            break;
        }
        i += 1;
    }
    println!("{}", i);
}
ANo error, prints 5
BRuntimeError: infinite loop
CSyntaxError: missing semicolon after i += 1
DSyntaxError: missing curly brace
Attempts:
2 left
๐Ÿ’ก Hint
Check if all statements end properly.
โ“ Predict Output
advanced
2:00remaining
Loop with labeled break
What will this Rust program print?
Rust
fn main() {
    let mut count = 0;
    'outer: for i in 1..=3 {
        for j in 1..=3 {
            if i + j == 4 {
                break 'outer;
            }
            count += 1;
        }
    }
    println!("{}", count);
}
A2
B6
C9
D3
Attempts:
2 left
๐Ÿ’ก Hint
The labeled break exits both loops when i+j == 4.
โ“ Predict Output
expert
2:00remaining
Complex loop with multiple control flow statements
What is the final value of `result` after running this Rust code?
Rust
fn main() {
    let mut result = 0;
    let mut i = 0;
    while i < 5 {
        i += 1;
        if i == 3 {
            continue;
        }
        if i == 4 {
            break;
        }
        result += i;
    }
    println!("{}", result);
}
A6
B5
C3
D4
Attempts:
2 left
๐Ÿ’ก Hint
Trace each iteration and note when continue and break happen.

Practice

(1/5)
1. What does the break statement do inside a Rust loop?
easy
A. It restarts the loop from the beginning.
B. It skips the current iteration and continues with the next one.
C. It pauses the loop until a condition is met.
D. It stops the loop immediately and exits it.

Solution

  1. Step 1: Understand the role of break in loops

    The break statement immediately stops the loop and exits it.
  2. Step 2: Compare with other loop controls

    continue skips to the next iteration, but break ends the loop entirely.
  3. Final Answer:

    It stops the loop immediately and exits it. -> Option D
  4. Quick Check:

    break stops loop = C [OK]
Hint: Remember: break = stop loop, continue = skip iteration [OK]
Common Mistakes:
  • Confusing break with continue
  • Thinking break pauses instead of stops
  • Assuming break restarts the loop
2. Which of the following is the correct syntax to skip the current iteration in a Rust for loop?
easy
A. skip;
B. continue;
C. break;
D. next;

Solution

  1. Step 1: Identify the keyword to skip iteration

    In Rust, continue; skips the rest of the current loop iteration and moves to the next.
  2. Step 2: Check other options for correctness

    break; stops the loop, skip; and next; are not valid Rust keywords for this purpose.
  3. Final Answer:

    continue; -> Option B
  4. Quick Check:

    Skip iteration = continue [OK]
Hint: Use continue to skip current loop iteration [OK]
Common Mistakes:
  • Using break instead of continue
  • Using non-existent keywords like skip or next
  • Omitting the semicolon
3. What will be the output of this Rust code?
let mut count = 0;
loop {
    count += 1;
    if count == 3 {
        break;
    }
    println!("{}", count);
}
medium
A. 1 2 3
B. 0 1 2
C. 1 2
D. 3

Solution

  1. Step 1: Trace the loop iterations and print statements

    Loop increments count from 0 to 1, prints 1; then 2, prints 2; when count is 3, break stops loop before printing.
  2. Step 2: Determine printed values

    Only 1 and 2 are printed before break stops the loop.
  3. Final Answer:

    1 2 -> Option C
  4. Quick Check:

    Prints before break = 1 2 [OK]
Hint: Break stops before printing count 3 [OK]
Common Mistakes:
  • Assuming count 3 is printed
  • Starting count from 1 instead of 0
  • Confusing break and continue effects
4. Identify the error in this Rust loop code:
for i in 1..5 {
    if i == 3 {
        continue
    }
    println!("{}", i);
}
medium
A. Missing semicolon after continue
B. Incorrect range syntax
C. println! macro is used incorrectly
D. continue cannot be used inside for loops

Solution

  1. Step 1: Check syntax of continue statement

    In Rust, statements like continue require a semicolon to end the statement.
  2. Step 2: Verify other parts of the code

    The range 1..5 and println! usage are correct; continue is valid inside for loops.
  3. Final Answer:

    Missing semicolon after continue -> Option A
  4. Quick Check:

    Statements need semicolon = A [OK]
Hint: Add semicolon after continue to fix syntax [OK]
Common Mistakes:
  • Forgetting semicolon after continue
  • Thinking continue is invalid in for loops
  • Misreading range syntax
5. Given this Rust code, what will be the final value of sum?
let mut sum = 0;
for i in 1..6 {
    if i % 2 == 0 {
        continue;
    }
    if i == 5 {
        break;
    }
    sum += i;
}
hard
A. 4
B. 5
C. 6
D. 9

Solution

  1. Step 1: Analyze loop iterations and conditions

    Loop runs i from 1 to 5 (exclusive 6). For even i, continue skips adding. For i == 5, break stops loop before adding.
  2. Step 2: Calculate sum for valid i values

    i=1 (odd, add 1), i=2 (even, skip), i=3 (odd, add 3), i=4 (even, skip), i=5 (break before add). sum = 1 + 3 = 4.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    Sum odd before 5 = 4 [OK]
Hint: Add only odd numbers before 5, skip evens [OK]
Common Mistakes:
  • Adding even numbers mistakenly
  • Including 5 in sum despite break
  • Confusing continue and break effects
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