break
break exits the nearest enclosing loop.
Surface Syntax#
break;
Notes:
breakis a statement, terminated by a semicolon.breakdoes not carry a value in the current language design; there is nobreak <expr>form.
Semantics#
When executed, break;:
- terminates the innermost enclosing loop (
loop,while, orfor), and - continues execution at the statement immediately following that loop.
In nested loops, break only exits the nearest loop:
fn main () -> int {
while true {
while true {
break; // exits the inner loop only
}
break; // exits the outer loop
}
return 0;
}
break does not exit the current function. Use return for that.
Type Checking Rules#
breakis only permitted inside a loop body.- A
breakoutside a loop is a type-check error (diagnostics,E2007).
Notes#
break;is accepted inside loops (loop,while, andfor) and lowered end-to-end.break;outside of a loop is rejected (E2007).
examples:
Common Pitfalls#
- Forgetting the semicolon (
breakis a statement). - Expecting
breakto return a value (not supported). - Using
breakoutside a loop (rejected,E2007).
Source repository · Edit this page · View Markdown