let else
Statements
For the common case of matching a pattern and returning from the function, use
let else
.
The “else” case must diverge (return
, break
, or panic - anything but falling
off the end of the block).
Speaker Notes
The rewritten version is:
More to Explore
- This early return-based control flow is common in Rust error handling code,
where you try to get a value out of a
Result
, returning an error if theResult
wasErr
. - If students ask, you can also demonstrate how real error handling code would
be written with
?
.