Control flow
Control flow in Rust is expression-oriented: if, match, and loop produce values. There is no truthiness; conditions are bool and nothing else.
Detailed Explanation
An if in Rust is not a statement that optionally runs a block. It is an expression. let label = if ready { "yes" } else { "no" }; is legal, and both branches must produce the same type. If you omit else and try to use the value, the missing branch has type (), so the if only type-checks when you did not need a value.
Conditions are strictly bool. if count { does not compile. if ptr { does not compile. There is no truthiness, no null-as-false, no empty-string-as-false. You write if count > 0 and if let Some(x) = maybe. That is deliberate. Implicit conversions from numbers and pointers to booleans are a classic source of bugs in C, and Rust refuses to participate.
else if chains work as you expect. For more than a couple of discrete cases, match is the tool, because the compiler will tell you when a variant is missing. if is for a boolean question or for if let when you care about one pattern and are willing to ignore the rest.
Loops are control flow too: loop, while, while let, and for. loop is the infinite loop and is also an expression; break value makes the loop evaluate to value. while is loop plus a condition. for is the iterator loop and is the one you want for walking a collection. There is no C-style for (i = 0; i < n; i++). You write for i in 0..n or you iterate the collection directly.
return exits the current function. break and continue talk to the innermost loop, or to a labelled one if you write 'outer: loop and break 'outer. Labels matter when a loop sits inside another loop and you need to leave the outer one.
What it costs: an if is a branch. On a hot path the CPU's predictor usually wins, but a data-dependent if inside a tight numeric loop is still the thing profilers point at. Prefer iterators and match on enums over boolean flags that encode a state machine; the enum is both clearer and easier for the compiler to see as exhaustive.
When you reach for if: a true boolean. When you reach for if let: one pattern, and the other cases can be skipped. When you reach for match: more than one pattern, or you want the compiler to keep you honest as the enum grows.
Code Examples
fn label(ready: bool, retries: u8) -> &'static str {
if !ready {
"blocked"
} else if retries == 0 {
"fresh"
} else {
"retrying"
}
}
fn main() {
println!("{}", label(true, 0)); // prints: fresh
let mut hops = 0;
let arrived = loop {
hops += 1;
if hops == 3 {
break true; // the loop expression evaluates to true
}
};
println!("{arrived} after {hops}"); // prints: true after 3
}Common Gotchas
Conditions must be `bool`
`if count` is `error[E0308]`. Write `if count != 0`. Rust will not coerce integers or `Option` to a boolean.
`if` branches used as a value must match
`let x = if c { 1 } else { "no" };` does not compile. Both sides need the same type, or you wrap them in an enum.
`if` without `else` is `()`
You cannot write `let x = if c { 1 };`. The missing else would have to be `()` and `1` is not `()`.
`break` can carry a value only from `loop`
`break 1` inside `while` or `for` is a compile error. Use `loop` when the loop itself should evaluate to something.