5.5 Pattern matching
match result {
Receipt r if r.amount > LIMIT => review(r)
Receipt r => ok(r)
Declined(reason) => warn(reason)
Timeout | Unavailable => retryLater()
_ => fail Unknown
}| Pattern | Example |
|---|---|
| literal, range | 0, "x", 1..=9 |
| placeholder | _ |
| binding | x, mutable binding: var x |
| type | Receipt, with a binding Receipt r |
| enum variant | Circle(r), .Circle(r), Rect(w: w, ..) |
| data | User{ name, role: .Admin, .. } |
| tuple | (a, _, c) |
| list/slice | [], [x], [first, ..rest] |
| alternative | A | B (the bindings are the same in all branches) |
| guard | pattern if condition |
A match MUST be exhaustive. Branches are separated by a line break or a comma; the body of a branch is an expression or a block.
An additional pattern: x? — matches any value except none and binds it to x (while let d? = queue.pop() { … }).
Related diagnostics
non-exhaustive, pattern-bindings, unreachable-arm
See also
Source: docs/spec/oxl-standard.en.md