Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Keywords and Statements

The break and continue keywords

Reference for Rust break and continue: leaving or skipping loop iterations, break with a value, labeled loops and labeled blocks, with type rules.

break ends the innermost loop immediately, and continue abandons the rest of the current iteration and jumps to the next one. Both apply to loop, while and for. Because Rust loops can nest, both accept an optional label ('name) so you can act on an outer loop without flags or helper functions.

In a loop, break can also carry a value that becomes the value of the loop expression. Since Rust 1.65, labeled blocks ('a: { ... }) can be left early with break 'a value, which gives you an early exit from a block without any loop.

Rust
for n in numbers {
    if n < 0 { continue; }
    if n > 100 { break; }
}

Try it

Rust Skip and stop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Labels and labeled blocks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
break;
break 'label;
break value;              // only inside `loop` or a labeled block
break 'label value;
continue;
continue 'label;
Form Description
break Exits the innermost enclosing loop. The loop expression then evaluates to () (for loop, when no value is given).
break value Exits a loop and makes value the result. Only valid in loop or labeled blocks.
break 'label Exits the loop or block carrying that label, which may be several levels up.
continue Skips to the next iteration of the innermost loop. In while the condition is checked again; in for the next item is fetched.
continue 'label Continues the next iteration of the labeled loop. Labeled blocks cannot be continued.

Errors: break outside a loop is E0268. break value inside while or for is E0571. Using an undeclared label is E0426. Mixing value types between several break statements in one loop is a type mismatch.

Usage notes

  • Labels are lexically scoped and start with a single quote. Rust warns if an inner label shadows an outer one with the same name.
  • continue in a while loop can cause an infinite loop if the counter update sits after it; update before continue, or restructure the loop as a for.
  • Closures are a boundary. break and continue cannot cross a closure, so inside iter().for_each(|x| ...) you cannot break out; use a for loop, try_for_each, or find.
  • Prefer iterator methods (find, position, take_while, any) over hand-written break when they say the same thing more directly.
  • Labeled blocks replace a pattern that used to need a loop { ...; break; } trick.
  • Destructors of locals run correctly when you leave a scope with break or continue.
  • break and return both have type !, so they can appear in match arms where a value is expected.