continue
continue skips the remainder of the current loop iteration and jumps to the
next iteration of the nearest enclosing loop.
Surface Syntax#
continue;
Notes:
continueis a statement, terminated by a semicolon.
Semantics#
When executed inside a loop body, continue;:
- stops executing the remainder of the current iteration’s body, and
- transfers control to the loop’s “next iteration” point:
- for
loop, this means jumping to the start of the loop body. - for
while, this means re-evaluating the loop condition. - for
for, this means advancing to the next iteration (and for C-styleforloops, executing the loop step before re-checking the loop condition).
Example:
fn main () -> int {
let mut i: int = 0;
while i < 10 {
i += 1;
if i == 5 {
continue; // skips the return below for i == 5
}
// More work could happen here.
}
return 0;
}
In nested loops, continue applies to the nearest loop:
fn main () -> int {
while true {
while true {
continue; // continues the inner loop
}
}
return 0;
}
Type Checking Rules#
continueis only permitted inside a loop body.- A
continueoutside a loop is a type-check error (diagnostics,E2008).
Notes#
continue;is accepted inside loops (loop,while, andfor) and lowered end-to-end.continue;outside a loop is rejected (E2008).
Example that uses continue in the Supported forms:
Common Pitfalls#
- Forgetting the semicolon (
continueis a statement). - Expecting
continueto exit the loop (it does not; usebreak). - Using
continueoutside a loop (rejected,E2008).
Source repository · Edit this page · View Markdown