← Back to Rust Course | Chapter 3: Control Flow | Lesson 6 of 7

Breaking with a Value

You can make a break hand back a result, so the loop itself becomes the answer to a question.

Returning a Value on Break

When a loop is exited using break some_value;, that value becomes the result of the entire loop expression, which can then be stored in a variable.

Example: Returning a Value on Break

markup
fn main() {
    let mut counter = 0;
    let result = loop {
        counter += 1;
        if counter == 10 {
            break counter;
        }
    };
    println!("Loop returned: {}", result);
}

Multiple break Points

A single loop can contain more than one break statement along different code paths, as long as every one of them produces a value of the same type.

Example: Multiple break Points

markup
fn main() {
    let mut n = 1;
    let outcome = loop {
        n *= 2;
        if n > 100 {
            break "too big";
        }
        if n == 64 {
            break "found sixty-four";
        }
    };
    println!("{}", outcome);
}

Why Only loop Supports This

while and for loops can also end because their condition became false or the iterator was exhausted, so there is no single guaranteed exit value to return. loop has no implicit exit, so break value is unambiguous.

Example: Why Only loop Supports This

markup
fn main() {
    let target = 5;
    let mut current = 0;
    let found_at = loop {
        current += 1;
        if current == target {
            break current;
        }
    };
    println!("Found target at iteration {}", found_at);
}

Combining with match on the Result

Because break value produces an ordinary value, you can immediately use it with other expressions, such as feeding it directly into a match.

Example: Combining with match on the Result

markup
fn main() {
    let mut i = 0;
    let parity = loop {
        i += 3;
        if i > 10 {
            break i % 2;
        }
    };
    match parity {
        0 => println!("Ended on an even number"),
        _ => println!("Ended on an odd number"),
    }
}
Common Mistakes
  1. Trying to use break value; inside a for or while loop -- only loop can return a value this way.
  2. Forgetting a semicolon after a loop expression when assigning its result, since the whole loop {} is treated as one expression.
  3. Mismatching the types produced by different break statements within the same loop, which fails to compile.
Chapter Summary
  • break value; only works inside loop, because only loop is guaranteed to be exited exclusively via break.
  • The value passed to break becomes the value of the entire loop expression.
  • All break statements within the same loop must produce values of the same type.
  • This pattern is common for retry-until-success logic where the result is only known once the loop ends.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.