Pattern matching

match scrutinises a value against a sequence of patterns and runs the first arm that matches. It is an expression: it returns the matched arm's value.

Basics

scratch.xs

The wildcard _ matches anything and discards the value. A bare name like n also matches anything but binds the value.

Pattern types

scratch.xs
scratch.xs
scratch.xs
scratch.xs
scratch.xs
scratch.xs

Guards

Append if condition after a pattern to add a guard. The arm only matches if the pattern fits and the guard is truthy.

scratch.xs

Or-patterns

scratch.xs

At-capture

n @ pattern tests the value against the pattern and also binds it to n.

scratch.xs

Exhaustiveness

The semantic analyser checks that all enum variants are covered. For non-enum types, a wildcard or variable pattern makes a match exhaustive. A match that cannot cover all cases is a compile-time error.