In let pat = value else { ... }, the else runs when the pattern doesn't match, and then
there are no values to bind. So it must leave the block (return, break, continue or
fail) rather than produce a fallback value.
Wrong#
fn head(xs: [int]): int {
let [h, ..t] = xs else 0
h
}Fixed#
fn head(xs: [int]): int {
let [h, ..t] = xs else { return 0 }
h
}Common causes#
- Writing a default value after
else: usereturn value, or amatchwith a fallback arm. - For a single
T?orT!value, a plainlet x = opt else 0works (the pattern is justx).