Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The continue keyword is a control flow expression used within loop constructs (loop, while, while let, and for) to immediately terminate the execution of the current iteration. When evaluated, it bypasses all subsequent statements in the current loop body and forces the control flow to jump directly to the next iteration’s evaluation phase. In Rust’s expression-oriented design, continue evaluates to the never type (!). Because the never type represents a computation that never resolves to a value, it can be safely coerced into any other type. This allows continue to be used in contexts that expect a specific type, such as within if or match expressions that are bound to a variable.

Basic Syntax and Type Coercion

fn main() {
    for x in 0..5 {
        // `continue` evaluates to `!`, which coerces to `i32` to match the `else` block
        let value: i32 = if x % 2 == 0 {
            continue; 
        } else {
            x * 10
        };
        
        println!("{}", value);
    }
}

Execution Mechanics by Loop Type

The exact behavior of the jump triggered by continue depends on the loop construct enclosing it:
  • for loops: Control flow jumps to the iterator’s next() method. If a new value is yielded, it is bound to the loop pattern, and the loop body executes again.
  • while loops: Control flow jumps back to the loop’s boolean condition. The condition is re-evaluated; if true, the loop body executes again.
  • while let loops: Control flow jumps back to the pattern matching evaluation. If the expression successfully matches the pattern again, the loop body executes.
  • loop constructs: Because there is no condition or iterator to evaluate, control flow jumps directly to the first statement at the top of the loop block.

Labeled continue

By default, continue applies only to the innermost enclosing loop. To alter the control flow of nested loops, Rust utilizes loop labels. A label is defined with a single quote followed by an identifier ('label_name:). When continue is paired with a label, it terminates the current iteration of the specified outer loop, bypassing the remaining execution of both the inner and outer loop bodies for that iteration.
fn main() {
    'outer: for x in 1..=3 {
        for y in 1..=3 {
            if x == y {
                // Terminates the current iteration of the 'outer loop.
                // The inner loop is aborted, and 'outer advances to the next `x`.
                continue 'outer; 
            }
            println!("x: {}, y: {}", x, y);
        }
    }
}

Scope and Constraints

  • Context Restriction: continue is strictly bound to loop execution contexts. Attempting to use it outside of a loop, while, while let, or for block results in a compile-time error (E0268).
  • Value Yielding: Unlike the break statement, which can return a value from a loop construct (e.g., break counter;), continue cannot carry or return a value. Its sole function is control flow redirection.
Master Rust with Deep Grasping Methodology!Learn More