E0571 Break With Value Fix

Written November 27, 2026 · Tested with Rust 1.99.1 · 20 min read

By the end of this guide
  • The error: a valued break in a while loop
  • The for form: the iterator can end the loop without you
  • The while let form: reported as a while loop
  • Labels do not change the loop kind

Written November 27, 2026

Tested with Rust 1.99.1

What this guide is

E0571 is the error for a break that carries a value out of a loop that cannot produce one. A loop is an expression: break value; ends it and makes value the value of the whole loop expression, which is how a search loop hands its result to the let that surrounds it. A while, for, or while let loop is not that kind of expression — each of them can also end on its own, when the condition turns false, the iterator runs out, or the pattern stops matching, and on those exits no break runs and there is no value to hand back. So the compiler refuses the valued break in those loops at compile time, with E0571: a break with value was used in a context where it is not allowed.

The repairs are a choice about where the value should travel instead. Convert the loop to a loop, keeping the valued break for the result and moving any condition that can still fire inside as an if that breaks without a value. Keep the loop and store the result in a variable declared before it, set in the iteration that breaks. Return the value from the enclosing function, when the loop is a search whose answer is the function's answer. Or, when the valued break exits nested loops through a label, make the labeled target a loop. The sections below take the shapes in order: the while form, the for and while let forms, the labeled forms, the cases where a valued break inside a while or for is nevertheless legal because its target is a loop, the four repairs, the labeled-block alternative, and the neighboring errors E0268, E0426, and E0308.

The error: a valued break in a while loop

This loop doubles n until it passes 40 and tries to hand that value out as the loop's result:

Rust
fn main() {
    let mut n = 1;
    let answer = while n < 100 {
        n *= 2;
        if n > 40 {
            break n;
        }
    };
    println!("answer is {:?}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0571]: `break` with value from a `while` loop
 --> main.rs:6:13
  |
3 |     let answer = while n < 100 {
  |                  ------------- you can't `break` with a value in a `while` loop
...
6 |             break n;
  |             ^^^^^^^ can only break with a value inside `loop` or breakable block
  |
help: use `break` on its own without a value inside this `while` loop
  |
6 -             break n;
6 +             break;
  |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0571`.

The diagnostic names both ends of the fault. The header states the loop kind — "break with value from a while loop" — the underline on the loop head reads "you can't break with a value in a while loop", and the underline on the break itself states the positive rule: "can only break with a value inside loop or breakable block". The help offers the compiler's minimal edit — drop the value and keep a plain break — which compiles but discards the result; it is the right edit only when the value was not needed. Note what does not matter to the error: whether the loop's result is used. The same valued break in a while loop written as a bare statement, with nothing assigned from it, fails with the identical E0571, and even break (); — a valued break whose value is the unit value — fails the same way, because the restriction is on carrying any value, not on the value's type.

The for form: the iterator can end the loop without you

A for loop has the same fault with its own header. Searching a range for the first square over 40 and trying to break out with the number:

Rust
fn main() {
    let answer = for x in 1..100 {
        if x * x > 40 {
            break x;
        }
    };
    println!("answer is {:?}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0571]: `break` with value from a `for` loop
 --> main.rs:4:13
  |
2 |     let answer = for x in 1..100 {
  |                  --------------- you can't `break` with a value in a `for` loop
3 |         if x * x > 40 {
4 |             break x;
  |             ^^^^^^^ can only break with a value inside `loop` or breakable block
  |
help: use `break` on its own without a value inside this `for` loop
  |
4 -             break x;
4 +             break;
  |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0571`.

The header reads "break with value from a for loop" and the head underline names the for loop; everything else is word-for-word the while printing. The reason is visible in the shape of the loop: if no element satisfies the condition, the range is exhausted and the loop ends with no break executed, so a variable assigned from the loop would have no value on that path. A loop has no such path — the only way out is a break — which is why it alone can promise a value.

The while let form: reported as a while loop

A while let loop is the third loop kind, and it prints under the while name. Popping values off a vector and trying to break out with the first one over 4:

Rust
fn main() {
    let mut vals = vec![3, 5, 8];
    let answer = while let Some(v) = vals.pop() {
        if v > 4 {
            break v;
        }
    };
    println!("answer is {:?}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0571]: `break` with value from a `while` loop
 --> main.rs:5:13
  |
3 |     let answer = while let Some(v) = vals.pop() {
  |                  ------------------------------ you can't `break` with a value in a `while` loop
4 |         if v > 4 {
5 |             break v;
  |             ^^^^^^^ can only break with a value inside `loop` or breakable block
  |
help: use `break` on its own without a value inside this `while` loop
  |
5 -             break v;
5 +             break;
  |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0571`.

The header and the head underline both say "while loop" — there is no separate "while let loop" wording in this diagnostic, so searching compiler output for the loop kind you wrote can mislead; the span lines are what identify the loop. The fault and the reason are the same: the loop also ends when the pattern stops matching — here, when pop returns None — and that exit carries no value.

Labels do not change the loop kind

Putting a label on the loop and naming it in the break changes which loop the break exits, not what kind of loop it is. A labeled while is still a while:

Rust
fn main() {
    let mut n = 1;
    let answer = 'outer: while n < 100 {
        n *= 2;
        if n > 40 {
            break 'outer n;
        }
    };
    println!("answer is {:?}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0571]: `break` with value from a `while` loop
 --> main.rs:6:13
  |
3 |     let answer = 'outer: while n < 100 {
  |                  --------------------- you can't `break` with a value in a `while` loop
...
6 |             break 'outer n;
  |             ^^^^^^^^^^^^^^ can only break with a value inside `loop` or breakable block
  |
help: use `break` on its own without a value inside this `while` loop
  |
6 -             break 'outer n;
6 +             break 'outer;
  |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0571`.

The printing is the plain form with the label carried through: the head underline spans the label and the loop head together, the break underline covers break 'outer n whole, and the help now offers break 'outer; — the labeled break with the value dropped. A labeled for loop fails identically:

Rust
fn main() {
    let answer = 'outer: for x in 1..100 {
        if x * x > 40 {
            break 'outer x;
        }
    };
    println!("answer is {:?}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0571]: `break` with value from a `for` loop
 --> main.rs:4:13
  |
2 |     let answer = 'outer: for x in 1..100 {
  |                  ----------------------- you can't `break` with a value in a `for` loop
3 |         if x * x > 40 {
4 |             break 'outer x;
  |             ^^^^^^^^^^^^^^ can only break with a value inside `loop` or breakable block
  |
help: use `break` on its own without a value inside this `for` loop
  |
4 -             break 'outer x;
4 +             break 'outer;
  |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0571`.

So a label is never itself the repair for E0571. Labels become part of a repair only when the labeled loop is, or becomes, a loop — the nested-loops repair below.

It is the target loop that counts, not the nearest loop

The restriction follows the loop the break actually exits. A valued break written inside a while loop is legal when a label aims it at an enclosing loop:

Rust
fn main() {
    let mut n = 1;
    let answer = 'outer: loop {
        while n < 100 {
            n *= 2;
            if n > 40 {
                break 'outer n;
            }
        }
    };
    println!("answer is {}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
answer is 64

The program prints answer is 64 and exits with status 0. The break 'outer n never tries to make the while loop produce a value; it exits the loop labeled 'outer, and the value becomes that loop's value. The mirror image also compiles: a valued break with no label inside a while loop, where the break belongs to a loop written inside the while body:

Rust
fn main() {
    let mut total = 0;
    let mut n = 0;
    while n < 3 {
        let step = loop {
            total += 2;
            if total > 3 {
                break total;
            }
        };
        println!("step is {}", step);
        n += 1;
    }
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
step is 4
step is 6
step is 8

The program prints the three step lines and exits with status 0, because each valued break targets the inner loop, which is allowed to yield its value to the let step around it. Reading an E0571, then, starts with identifying the target: the innermost enclosing loop for an unlabeled break, or the labeled loop for a labeled one. Only when that target is a while, for, or while let does the error fire.

First fix: convert the loop to a loop

When the value is the point — the loop exists to produce it — the direct repair is to make the loop a loop. An old condition that can still fire moves inside as an exit test that breaks without a value; a condition the valued break always reaches first can be dropped, and this example is that second case. The while example becomes:

Rust
fn main() {
    let mut n = 1;
    let answer = loop {
        n *= 2;
        if n > 40 {
            break n;
        }
    };
    println!("answer is {}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
answer is 64

The program prints answer is 64 and exits with status 0. The conversion drops the old n < 100 condition because it could never fire first: doubling from 1 reaches 64, the first value over 40, while n is still under 100, so the valued break always ends this loop before the condition can. Where a condition could fire first, it must be carried inside as a plain-break exit test, or the converted loop runs longer than the original. The conversion is mechanical for all three loop kinds. A while cond becomes a loop whose body ends with if !cond { break; } or begins with the positive test restructured; a while let becomes a loop with a match that breaks on the non-matching arm; a for becomes a loop driving next() on the iterator by hand, breaking when it returns None. Those restructurings are the cost of this fix: the loop head no longer states the iteration in one line, which is why it is the right repair when the result matters more than the loop's shape, and the wrong one for a loop that merely happens to compute something on its way out — that is the next fix.

Second fix: set a variable before a plain break

When the loop's own shape is the clear one — a condition in the head, an iterator driving it — keep the loop, declare the result variable before it, and assign in the iteration that breaks:

Rust
fn main() {
    let mut n = 1;
    let mut answer = 0;
    while n < 100 {
        n *= 2;
        if n > 40 {
            answer = n;
            break;
        }
    }
    println!("answer is {}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
answer is 64

The program prints answer is 64 and exits with status 0. The valued break becomes the plain break the compiler's help suggests, and the value travels through the variable instead of through the loop expression. Two details decide whether this fix is complete. The variable needs a value for the path where the loop ends on its own — the initial 0 here is the "not found" answer, and choosing it is part of the repair, not boilerplate: it is exactly the value the loop-expression version could never produce, and the code now has to say what it means. And the assignment and the break must stay together; assigning and continuing to iterate overwrites the result on a later pass.

Third fix: return the value from the function

When the loop is a search and its result is the enclosing function's result, return carries the value out of both at once, and the loop keeps whatever form suits it:

Rust
fn first_over(limit: i32) -> i32 {
    for x in 1..100 {
        if x * x > limit {
            return x;
        }
    }
    0
}

fn main() {
    println!("answer is {}", first_over(40));
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
answer is 7

The program prints answer is 7 and exits with status 0. The for loop is untouched — no conversion, no result variable — because return exits the function, not the loop, and a function's return value is an ordinary channel that no loop kind restricts. The trailing 0 after the loop is the not-found answer for the path where the range is exhausted, the same decision the variable fix makes explicit. This is the repair to reach for when the caller wants the value and nothing after the loop in the function needs to run on the found path; when code after the loop must run with the result in hand, the variable fix is the shape that keeps it in the same function.

Fourth fix: make the labeled target a loop, for nested loops

A valued break that exits nested loops through a label fails when the labeled loop is a while — the inner loop's kind is irrelevant, the target is what counts:

Rust
fn main() {
    let mut rounds = 0;
    let answer = 'outer: while rounds < 3 {
        rounds += 1;
        for x in 1..100 {
            if x * x > 40 {
                break 'outer x;
            }
        }
    };
    println!("answer is {:?}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0571]: `break` with value from a `while` loop
 --> main.rs:7:17
  |
3 |     let answer = 'outer: while rounds < 3 {
  |                  ------------------------ you can't `break` with a value in a `while` loop
...
7 |                 break 'outer x;
  |                 ^^^^^^^^^^^^^^ can only break with a value inside `loop` or breakable block
  |
help: use `break` on its own without a value inside this `while` loop
  |
7 -                 break 'outer x;
7 +                 break 'outer;
  |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0571`.

Converting the labeled loop to a loop — and moving its round limit inside as a plain-break exit — makes the same labeled valued break legal:

Rust
fn main() {
    let answer = 'outer: loop {
        for x in 1..100 {
            if x * x > 40 {
                break 'outer x;
            }
        }
    };
    println!("answer is {}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
answer is 7

The program prints answer is 7 and exits with status 0. The label and the break 'outer x are unchanged; only the target's kind moved. A labeled loop taking a value directly is the same mechanism without the nesting:

Rust
fn main() {
    let mut n = 1;
    let answer = 'outer: loop {
        n *= 2;
        if n > 40 {
            break 'outer n;
        }
    };
    println!("answer is {}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
answer is 64

That program prints answer is 64 and exits with status 0 — break 'outer n names the loop it exits and supplies its value in one statement.

The labeled-block alternative: a breakable block

The break underline's wording — "inside loop or breakable block" — names a second legal target. A labeled block is an expression that a labeled break can exit with a value, so wrapping the search in one lets a for loop stay a for loop while its result still travels on a break:

Rust
fn main() {
    let answer = 'block: {
        for x in 1..100 {
            if x * x > 40 {
                break 'block x;
            }
        }
        0
    };
    println!("answer is {}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
answer is 7

The program prints answer is 7 and exits with status 0. The break 'block x exits the block, not the for loop, and its value becomes the block's value; the 0 after the loop inside the block is the not-found value for the exhausted-range path, which the block form, like the variable and return fixes, makes the code state explicitly. This is the least invasive repair when the loop is nested inside an expression and neither converting it nor restructuring the function around it is attractive.

Neighboring error: a break outside any loop is E0268

A break with no loop around it at all is a different fault with a different code:

Rust
fn main() {
    break;
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0268]: `break` outside of a loop or labeled block
 --> main.rs:2:5
  |
2 |     break;
  |     ^^^^^ cannot `break` outside of a loop or labeled block

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0268`.

That is E0268: "break outside of a loop or labeled block". There is no loop head to underline and no value in the statement — the fault is the missing loop itself, not a value a loop cannot carry. The split is positional: break outside every loop or labeled block is E0268 whatever it carries; break with a value inside a loop whose target cannot yield one is E0571.

Neighboring error: an undeclared label is E0426

A break or continue that names a label no enclosing loop declares fails before the value question is ever reached:

Rust
fn main() {
    loop {
        break 'missing;
    }
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0426]: use of undeclared label `'missing`
 --> main.rs:3:15
  |
3 |         break 'missing;
  |               ^^^^^^^^ undeclared label `'missing`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0426`.

That is E0426, use of undeclared label — a general label error, not a break-specific one. The same code prints for continue 'missing; inside a loop, and for break 'missing 42; inside a loop: adding a value does not turn it into E0571, because the compiler cannot check a valued break against a target loop it cannot find. When a labeled break reports E0426, the repair is on the label — declare it on the loop the break is meant to exit, or correct the spelling — and only after the label resolves does the target's kind decide whether a value may travel on it.

Neighboring error: two break values of different types in one loop are E0308

Inside a genuine loop, a valued break is legal — but every break that exits the same loop must agree on the value's type, because together they define the type of the loop expression. Two breaks carrying different types fail, and not with E0571:

Rust
fn main() {
    let mut n = 0;
    let answer = loop {
        n += 1;
        if n > 2 {
            break n;
        }
        if n > 1 {
            break "done";
        }
    };
    println!("answer is {}", answer);
}
Terminal
$ rustc --edition 2021 main.rs -o main.bin
error[E0308]: mismatched types
 --> main.rs:9:19
  |
6 |             break n;
  |             ------- expected because of this `break`
...
9 |             break "done";
  |                   ^^^^^^ expected integer, found `&str`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.

That is E0308, mismatched types: the second break's value is checked against the type the first break established, "expected because of this break". So E0571 and E0308 divide the valued-break faults by loop kind: in a while, for, or while let loop, a valued break is E0571 whatever its type; in a loop, a valued break is fine until its type disagrees with another break's, which is E0308. A single break whose value's type disagrees with the type annotated for the variable the loop is assigned to is likewise E0308, at the assignment.

The exact rule: only loop and labeled blocks evaluate to a value

The official page for E0571 defines the fault in one sentence: a break statement with an argument appeared in a non-loop loop. Its erroneous example is the while form — a while true loop breaking with 2 * i — and its stated repair is to make sure break value; statements only occur in loop loops, demonstrated by converting the example's while to a loop. The page carries no note retiring or replacing this code; E0571 is the current code for this fault.

The mechanism is in the loop expressions themselves. The Reference's page on loop expressions lists four loop forms — loop, while, and for, alongside labeled block expressions (while let is a while whose condition is a pattern, not a separate form) — and states that only loop and labeled block expressions support evaluation to non-trivial values: a while expression evaluates to (), and a for expression completes when its iterator is empty. A break expression may return a value from a loop via break EXPR or break 'label EXPR when it is associated with a loop, and the type of a loop with break expressions is the least upper bound of all the break operands — which is why the breaks in one loop must agree in type. The Book's control-flow chapter presents the same rule from the user's side: add the value after the break expression used to stop a loop and that value is returned out of the loop, return exits the current function rather than the current loop, and a loop label lets break and continue apply to the labeled loop instead of the innermost one.

The fixes, ranked

  • First, convert the loop to a loop, when the loop exists to produce the value and the result is assigned straight from it. The valued break survives unchanged; a loop-head condition that can still fire becomes an exit test inside the body.
  • Second, set a result variable before a plain break, when the loop's head — a condition or an iterator — states the iteration better than a loop would. Decide the variable's initial value as the not-found answer; that decision is part of the fix.
  • Third, return the value from the enclosing function, when the loop is a search whose answer is the function's answer and nothing after the loop needs the found value. The loop itself needs no edit at all.
  • Fourth, make the labeled target a loop, when the valued break exits nested loops through a label. Only the target's kind changes; the label and the break stay as written. Where the nesting sits inside an expression, a labeled block around the loop achieves the same channel without converting anything.

Which fix for which cause

  • A while loop assigned to a variable breaks with the value that variable should hold: E0571, header "break with value from a while loop". Convert to a loop, or set the variable before a plain break.
  • A for loop over a range or collection breaks with the element or index it found: E0571, header naming the for loop. return it if the loop is a function-level search; otherwise the variable fix, a loop driving the iterator, or a labeled block.
  • A while let loop breaks with the value it just matched: E0571, printed as a "while loop" in the header. Convert to a loop with a match, or take the value into a variable before breaking.
  • The break carries a label and the labeled loop is a while or for: still E0571 — the label selects the target, it does not change the target's kind. Make the labeled loop a loop.
  • The break carries a label naming an enclosing loop, written inside an inner while or for: no error. The target is a loop, and the value becomes its value.
  • A valued break sits in a while statement whose result nobody assigns, or carries only (): still E0571. The restriction is on carrying a value at all, not on using the result.
  • A break or continue names a label that no enclosing loop declares, with or without a value: E0426, use of undeclared label. Fix the label; the value question is not reached.
  • A break appears outside every loop and labeled block: E0268. There is no target at all; the loop or block has to exist before any break form is legal.
  • Two breaks out of one loop carry values of different types, or a break's value disagrees with the annotated type of the variable the loop is assigned to: E0308, mismatched types. Unify the break values' types — inside a loop, the value channel itself is legal.

Checklist

  • Identify the target loop before editing: the innermost enclosing loop for an unlabeled break, the labeled loop for a labeled one. E0571 fires only when that target is a while, for, or while let.
  • Read the header for the target's kind as the compiler names it — a while let loop is reported as a while loop.
  • Do not take the compiler's help at face value when the value matters. Dropping the value compiles, and silently discards the result the break was carrying; take it only when the value was genuinely unneeded.
  • Decide the not-found value wherever the fix introduces one. The variable, return, and labeled-block fixes all require a value for the path where the loop ends on its own; the loop conversion is the only fix that never needs one.
  • Keep the assignment and the break together in the variable fix — a value set in an iteration that does not break is overwritten by the next pass.
  • Check the neighbouring codes before restructuring: E0426 means the label does not resolve, E0268 means there is no loop, and E0308 inside a loop means the break values disagree with each other — none of the three is repaired by converting a loop kind.

Sources

  • Rust Compiler Error Index, E0571 — for a break statement with an argument appearing in a non-loop loop, for the erroneous example of a while loop breaking with a value, and for making sure break value; statements only occur in loop loops.
  • The Rust Reference, Loop expressions — for the four loop forms, for only loop and labeled block expressions supporting evaluation to non-trivial values, for a while expression evaluating to (), and for the type of a loop being the least upper bound of its break operands.
  • The Rust Programming Language, Control Flow — for returning a value from a loop by adding it after the break expression, for return exiting the current function rather than the current loop, and for loop labels applying break and continue to the labeled loop.
  • Rust Compiler Error Index, E0268 — for a loop keyword, break or continue, being used outside of a loop.
  • Rust Compiler Error Index, E0426 — for an undeclared label being used, and for verifying that the label was spelled and declared correctly.

Keep Exploring