lotsoftools

Understanding Rust Error E0696: Incorrect use of continue keyword

Introduction to Rust Error E0696

Rust Error E0696 is an error related to the incorrect usage of the continue keyword in your Rust code. The continue keyword is used to skip the current iteration of a loop and proceed to the next one. This error occurs when continue is used with a labeled block instead of a loop, or when used with a loop label that does not exist.

Erroneous code examples

Let's examine the following erroneous code examples:

Incorrect continue usage in a simple block:

fn continue_simple() {
    'b: {
        continue; // error!
    }
}

Incorrect continue usage with a label:

fn continue_labeled() {
    'b: {
        continue 'b; // error!
    }
}

Incorrect continue usage crossing loops:

fn continue_crossing() {
    loop {
        'b: {
            continue; // error!
        }
    }
}

Fixing the errors

To fix the errors, we must ensure that the continue keyword is used within a loop construct. Here are the corrected examples:

Correct continue usage in a simple loop:

fn continue_simple() {
    'b: loop {
        continue; // ok!
    }
}

Correct continue usage with a loop label:

fn continue_labeled() {
    'b: loop {
        continue 'b; // ok!
    }
}

Correct continue usage with nested loops:

fn continue_crossing() {
    loop {
        'b: loop {
            continue; // ok!
        }
    }
}