1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-10-09 19:12:06 +02:00
Files
comprehensive-rust/src/control-flow-basics/break-continue.md
Martin Geisler 041fa6b9c2 docs: improve language in control-flow-basics section (#2887)
I asked Gemini to review the English for inconsistencies and grammar
mistakes. This is the result and I hope it's useful!

As a non-native speaker, it is hard for me to evaluate the finer
details, so let me know if you would like to see changes (or even
better: make them directly in the PR with the suggestion function).

---------

Co-authored-by: Dmitri Gribenko <gribozavr@gmail.com>
2025-09-06 16:49:57 +00:00

911 B

minutes
minutes
4

break and continue

If you want to immediately start the next iteration use continue.

If you want to exit any kind of loop early, use break. With loop, this can take an optional expression that becomes the value of the loop expression.

fn main() {
    let mut i = 0;
    loop {
        i += 1;
        if i > 5 {
            break;
        }
        if i % 2 == 0 {
            continue;
        }
        dbg!(i);
    }
}

Note that loop is the only looping construct that can return a non-trivial value. This is because it's guaranteed to only return at a break statement (unlike while and for loops, which can also return when the condition fails).