Error codes

E0240: the else of a refutable let must diverge

In a terminal: auk explain E0240 prints this page.

On this page

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#

auk
fn head(xs: [int]): int {
  let [h, ..t] = xs else 0
  h
}

Fixed#

auk
fn head(xs: [int]): int {
  let [h, ..t] = xs else { return 0 }
  h
}

Common causes#

  • Writing a default value after else: use return value, or a match with a fallback arm.
  • For a single T? or T! value, a plain let x = opt else 0 works (the pattern is just x).

Stuck on a word? Try the glossary. Got a compiler error? Look up its code. Improve this page