`while let`
while let repeats a block as long as a pattern matches. It is the loop form of if let, and it is how you drain iterators and channels without a manual match.
Detailed Explanation
while let Some(item) = iter.next() { ... } keeps calling next until it gets None. Each successful match binds item for one iteration. When the pattern fails, the loop ends. That is the entire construct: a while whose condition is a pattern, not a bool.
The usual sources are iterators and receiving ends. while let Some(line) = lines.next() reads a file-like iterator. while let Ok(msg) = rx.recv() drains a channel until the senders are gone and recv returns Err. while let Some(event) = pump.poll() is the same shape for a queue you control.
You could write loop { match iter.next() { Some(item) => ..., None => break } }. while let is that loop with the None => break baked in. Like if let, it is not exhaustive. It cares about one pattern. If you need to distinguish Ok from two different Errs inside the loop, use loop plus match.
The expression on the right is evaluated every iteration. That matters for recv(), for pop(), for anything with side effects. It also matters for binding: while let Some(x) = maybe on an Option that is not updated inside the body is an infinite loop if it starts as Some, because the right-hand side is the same value every time. while let is not "while this variable is Some". It is "while this expression matches".
let-else does not have a loop form. To skip a miss and keep going you write the match yourself, or you filter the iterator so the miss never appears: for item in iter.flatten() on an iterator of Option.
What it costs: the same as the equivalent loop/match. The cost that bites is locking or receiving inside the condition without a timeout, which can block the thread forever. Use a timed recv or an async stream when the source might stall.
When you reach for while let: draining something that yields Option or Result until a terminal miss. When you reach for for: the value implements IntoIterator and you do not need the terminal None to mean anything special. When you reach for loop: several patterns, or a break value.
Code Examples
fn main() {
let mut stack = vec!["ada", "grace", "alan"];
// `pop` yields Some until the vec is empty, then None.
while let Some(name) = stack.pop() {
println!("{name}");
}
// prints:
// alan
// grace
// ada
println!("{}", stack.len()); // prints: 0
}Common Gotchas
An unchanged `Option` loops forever
`while let Some(x) = maybe` with a `Copy` `Option` that you never set to `None` never stops. Update the source, or use `if let`.
`recv` without a timeout can block
`while let Ok(msg) = rx.recv()` waits forever if a sender is alive but silent. That is deadlock-shaped if the sender is waiting on you.
`for` is usually clearer for iterators
`for item in iter` is `while let Some(item) = iter.next()` with better syntax. Use `while let` when you call `next`/`pop`/`recv` yourself.
The pattern is not exhaustive
A new `Result` error variant will not be distinguished. Use `loop` plus `match` when the miss has more than one meaning.