Skip to main content

Pattern matching

match expression

match scrutinee '{' pattern '=>' result ( ',' pattern '=>' result )* ','? '}'

An optional if guard may appear after a pattern and before => where supported.

Pattern forms

PatternExample / note
Wildcard_ matches anything
Bindingbare name binds the value
Literalnumbers, strings, booleans
Tuple( p1, p2, … )
Struct-likefields with sub-patterns
Enum variantqualified name with optional inner pattern
Alternativesp1 | p2

Trailing commas after the last arm are allowed.

Result expression

Each arm’s result is a full expression: blocks, calls, nested match, casts, etc.

See also Expressions.