Bird
Raised Fist0
Rustprogramming~20 mins

Loop with break in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of a loop with break returning a value
What is the output of this Rust program?
Rust
fn main() {
    let mut count = 0;
    let result = loop {
        count += 1;
        if count == 5 {
            break count * 2;
        }
    };
    println!("{}", result);
}
A0
B5
C10
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
The loop breaks when count reaches 5 and returns count * 2.
โ“ Predict Output
intermediate
2:00remaining
Loop with break inside nested loops
What will be printed by this Rust code?
Rust
fn main() {
    let mut i = 0;
    let mut j = 0;
    'outer: loop {
        i += 1;
        loop {
            j += 1;
            if j == 3 {
                break 'outer;
            }
        }
    }
    println!("i = {}, j = {}", i, j);
}
ACompilation error
Bi = 3, j = 3
Ci = 1, j = 1
Di = 1, j = 3
Attempts:
2 left
๐Ÿ’ก Hint
The break with label exits the outer loop when j reaches 3.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in loop with break
What error does this Rust code produce?
Rust
fn main() {
    let mut x = 0;
    let y = loop {
        x += 1;
        if x > 3 {
            break x;
        }
    };
    println!("{}", y);
}
ASyntaxError: missing semicolon after x += 1
BTypeError: cannot break with a value
CNo error, prints 4
DRuntimeError: infinite loop
Attempts:
2 left
๐Ÿ’ก Hint
Check punctuation carefully inside the loop.
โ“ Predict Output
advanced
2:00remaining
Loop with break returning a tuple
What is the output of this Rust program?
Rust
fn main() {
    let mut a = 0;
    let mut b = 1;
    let result = loop {
        if a > 3 {
            break (a, b);
        }
        a += 1;
        b += 2;
    };
    println!("{:?}", result);
}
A(3, 7)
B(4, 9)
C(4, 5)
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
The loop breaks when a becomes greater than 3, returning (a, b).
๐Ÿง  Conceptual
expert
2:00remaining
Behavior of break in infinite loop with condition
Consider this Rust code snippet: fn main() { let mut n = 0; loop { n += 1; if n == 10 { break; } } println!("{}", n); } What is the value printed and why?
A10, because the loop breaks exactly when n reaches 10
B9, because the break happens before incrementing n to 10
CCompilation error due to missing break value
DInfinite loop, because break has no value
Attempts:
2 left
๐Ÿ’ก Hint
The break without a value just exits the loop; n increments before the check.

Practice

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

Solution

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

    The break keyword is used to stop the loop immediately, no matter where it is in the loop.
  2. Step 2: Compare with other loop controls

    Unlike continue which skips to the next iteration, break exits the loop completely.
  3. Final Answer:

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

    break = stop loop [OK]
Hint: Remember: break means stop the loop now [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 break out of a loop in Rust?
easy
A. break()
B. exit loop;
C. break;
D. stop;

Solution

  1. Step 1: Recall Rust loop break syntax

    In Rust, to exit a loop, you simply write break; with a semicolon.
  2. Step 2: Check other options for syntax errors

    break() is invalid because break is a keyword, not a function. exit loop; and stop; are not Rust keywords.
  3. Final Answer:

    break; -> Option C
  4. Quick Check:

    Correct break syntax = break; [OK]
Hint: Use break with semicolon to exit loops [OK]
Common Mistakes:
  • Using break as a function call
  • Writing break without semicolon
  • Using non-existent keywords like exit or stop
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
B. 1 2 3
C. 3
D. 0 1 2

Solution

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

    Loop starts with count = 0. Each loop adds 1 to count. When count reaches 3, break stops the loop. The print happens only if count is not 3.
  2. Step 2: List printed values

    When count=1, prints 1; count=2, prints 2; count=3 triggers break before print, so 3 is not printed.
  3. Final Answer:

    1 2 -> Option A
  4. Quick Check:

    Print before break stops at 3, so output is 1 2 [OK]
Hint: Break stops before printing 3, so only 1 and 2 print [OK]
Common Mistakes:
  • Assuming break prints the last value
  • Counting from 0 instead of 1
  • Ignoring that break stops loop before print
4. Identify the error in this Rust code snippet:
let mut i = 0;
loop {
    if i == 5 {
        break
    }
    println!("{}", i);
    i += 1;
}
medium
A. Missing semicolon after break
B. Variable i is not mutable
C. println! macro is used incorrectly
D. Loop condition is missing

Solution

  1. Step 1: Check syntax around break statement

    In Rust, statements must end with a semicolon. The break statement here lacks a semicolon.
  2. Step 2: Verify other parts of the code

    Variable i is mutable, println! is correctly used, and loop without condition is valid in Rust.
  3. Final Answer:

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

    break needs semicolon to avoid syntax error [OK]
Hint: Always put semicolon after break [OK]
Common Mistakes:
  • Forgetting semicolon after break
  • Thinking loop needs condition
  • Misreading println! syntax
5. You want to sum numbers from 1 to 10 but stop adding when the sum reaches or exceeds 20. Which Rust code correctly uses a loop with break to do this?
hard
A. let mut sum = 0; let mut n = 1; loop { sum += n; if sum >= 20 { break; } n += 1; } println!("{}", sum);
B. let mut sum = 0; for n in 1..=10 { sum += n; if sum >= 20 { break; } } println!("{}", sum);
C. let mut sum = 0; let mut n = 1; while n <= 10 { sum += n; if sum >= 20 { break; } n += 1; } println!("{}", sum);
D. let mut sum = 0; let mut n = 1; loop { if n > 10 { break; } sum += n; if sum >= 20 { break; } n += 1; } println!("{}", sum);

Solution

  1. Step 1: Understand the problem requirements

    Sum numbers from 1 to 10, but stop if sum reaches or exceeds 20. Use loop with break.
  2. Step 2: Analyze each option for correctness

    let mut sum = 0; let mut n = 1; loop { sum += n; if sum >= 20 { break; } n += 1; } println!("{}", sum); breaks after adding sum >= 20 but does not stop when n > 10, so it does not enforce the limit to 10. let mut sum = 0; for n in 1..=10 { sum += n; if sum >= 20 { break; } } println!("{}", sum); uses for loop, not loop. let mut sum = 0; let mut n = 1; while n <= 10 { sum += n; if sum >= 20 { break; } n += 1; } println!("{}", sum); uses while loop, not loop. let mut sum = 0; let mut n = 1; loop { if n > 10 { break; } sum += n; if sum >= 20 { break; } n += 1; } println!("{}", sum); correctly breaks if n > 10 or sum >= 20, stopping properly.
  3. Final Answer:

    Correctly uses loop with break to stop at sum >= 20 or n > 10. -> Option D
  4. Quick Check:

    let mut sum = 0; let mut n = 1; loop { if n > 10 { break; } sum += n; if sum >= 20 { break; } n += 1; } println!("{}", sum); [OK]
Hint: Check all break conditions to avoid infinite loops [OK]
Common Mistakes:
  • Missing break for loop exit condition
  • Using wrong loop type
  • Not checking both sum and count limits
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