Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Keywords and Statements

The loop expression

Reference for Rust loop: the unconditional loop, leaving it with break, returning a value from break, loop labels and retry patterns.

loop repeats its block forever, until the code inside leaves with break, return, a panic or process exit. It is the loop to use when there is no natural condition at the top: retry until success, run an event loop, or search until something is found.

Uniquely among Rust's loops, loop is an expression whose value is whatever you pass to break value. Since the compiler knows the loop only ends through break, code after a loop that never breaks is unreachable, and a function ending in such a loop can have return type !.

Rust
let result = loop {
    counter += 1;
    if counter == 5 { break counter * 10; }
};

Try it

Rust break with a value

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 Labeled loops

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
loop { body }
let value = loop { ... break expression; ... };
'label: loop { ... break 'label; ... continue 'label; ... }
let v = 'label: { ... break 'label value; ... };   // labeled block
Part Description
break Leaves the innermost loop. Plain break; gives ().
break value Leaves the loop and makes the whole loop expression evaluate to value. Every break of that loop must produce the same type.
'label: Names a loop so inner loops can break 'label or continue 'label it. Labels start with an apostrophe.

Type: a loop without any break has type ! (it never finishes). break value is only allowed in loop, not in while or for; using it there is error E0571.

Usage notes

  • Prefer while or for when a clear condition or collection exists. Use loop when the exit happens in the middle of the body.
  • Guarantee progress. A forgotten break is an infinite loop; give retry loops a maximum attempt count or a timeout.
  • Values via break replace the "mutable variable plus flag" pattern: the loop's result is bound right at let.
  • Labels avoid flag variables in nested loops. Labeled blocks ('a: { ... break 'a value; }, stable since Rust 1.65) give the same early exit without any loop.
  • Event loops and servers commonly use loop { match rx.recv() { ... } }, breaking when the channel closes.
  • Overflow: debug builds panic on arithmetic overflow, so an unbounded counter in a loop eventually panics rather than wrapping.
  • Optimization: the compiler treats loop and while true alike, but loop keeps the type system honest and avoids a warning.