thecodex.expert · The Codex Family of Knowledge
Rust

Control Flow

break can return a value from a loop in Rust — an unusual feature most languages don’t have at all.

Rust 1.96 if is an expression Last verified:
Canonical Definition

Rust’s if is an expression, not just a statement — it can produce a value usable directly in a let binding, provided every branch produces the same type. Rust has three loop keywords: loop (infinite, until an explicit break), while (condition-based), and for (iterating a range or collection) — and uniquely, break inside a loop can carry a value out of the loop entirely.

if as an expression

Because if/else is an expression, it can be assigned directly — but every branch must evaluate to the same type, or it's a compile error.

Rustif_expression.rs
let condition = true;
let number = if condition { 5 } else { 6 };   // if is an EXPRESSION here
println!("{}", number);   // 5

// let bad = if condition { 5 } else { "six" };  // COMPILE ERROR: mismatched types

Three loops: loop, while, for

for is idiomatic for iterating a known range or collection; while loops on a condition; loop repeats forever until an explicit break.

Rustloops.rs
for i in 0..5 {              // idiomatic: range-based, exclusive of 5
    println!("{}", i);
}

let mut n = 0;
while n < 3 {                 // condition-based
    n += 1;
}

loop {                         // infinite until break
    println!("runs once");
    break;
}

break can return a value from loop

This is a genuinely unusual feature: break inside a loop (only loop, not while or for) can carry a value, making the whole loop itself usable as an expression — a natural fit for retry logic that needs the successful result.

Rustloop_value.rs
let mut counter = 0;

let result = loop {
    counter += 1;
    if counter == 10 {
        break counter * 2;   // exits the loop AND produces a value
    }
};

println!("{}", result);   // 20

Sources

1
The Rust Project. The Rust Programming Language, "Control Flow", doc.rust-lang.org/book/ch03-05.