keyword

Rust break: leave a loop

Use `break` to stop a loop before its normal end. It is useful once a search finds what it needs or a limit has been reached. Expected output: 1 2 Why: The loop exits before printing 3. Common mistake: An unlabeled break exits the innermost enclosing loop, not every nested loop.

Formal Syntax & Structure

rust syntaxrust
break;

Working Example

Example Snippetrust
fn main() {    for n in 1..=5 {        if n == 3 {            break;        }        println!("{n}");    }}
Rust Reference