Skip to content

§8 Pattern Matching

Literal patterns, _, guards, and enum-variant destructuring lower end-to-end. Compile-time exhaustiveness checking remains partial, so emitted matches retain a runtime backstop for an uncovered value.

match value {
0 => print("zero"),
n if n < 0 => print("negative"),
Response.Ok { value } => process(value),
Response.Error { code, message } => print.err("{{ code }}: {{ message }}"),
_ => print("other"),
}

Pattern forms:

  • Literal0, "hello", true
  • Variable capture — any identifier binds the value
  • Wildcard_ matches anything, discards
  • Enum destructuringVariant { field } extracts payload fields
  • Guardpattern if condition adds a boolean filter
  • Exhaustiveness — compile-time checking is partial; use a _ wildcard when the known variants do not cover every possible value