Skip to content

§8 Pattern Matching

Note: match is parsed by the compiler and emits IR, but LLVM lowering of numeric and enum variant patterns may be unreliable in the current release. String and wildcard patterns are more stable. See the stability note in §4.

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 — the compiler requires all cases be covered (or a _ wildcard)