Contents

Pattern matching inspects sum types, enums, Option, and Result. Records and lists are destructured with field access and indexing.

Match Expressions#

A match names a subject and a sequence of arms. Each arm contains a pattern, =>, and a body. Arms are tested in order.

type Shape =
    | Circle(radius f64)
    | Rect(width f64, height f64)

fn area(shape Shape) f64 => match shape {
    Circle(radius) => 3.14159 * radius * radius
    Rect(width, height) => width * height
}

An expression match requires every arm to produce a value of a compatible type. A statement match may have arms that produce no value.

fn describe(value Option[i32]) {
    match value {
        Some(v) => print(v)
        None => print("missing")
    }
}

Constructors and Bindings#

A sum pattern names a variant and binds each field to a name. The number of bindings must match the number of fields. Bindings are immutable and scoped to the arm.

type Tree =
    | Leaf
    | Node(left Tree, value i32, right Tree)

fn sum(tree Tree) i32 => match tree {
    Leaf => 0
    Node(left, value, right) => sum(left) + value + sum(right)
}

An underscore in a binding position discards that field. A bare underscore is the wildcard arm, which matches any remaining value.

fn first_or(tree Tree, fallback i32) i32 => match tree {
    Node(_, value, _) => value
    _ => fallback
}

Without a wildcard, every variant must be covered. The compiler rejects a match that leaves a variant unhandled.

Enum Patterns#

Enum variants are qualified by the enum type in patterns.

type Color enum {
    Red
    Green
    Blue
}

fn code(colour Color) i32 => match colour {
    Color.Red => 1
    Color.Green => 2
    Color.Blue => 3
}

Enum variants carry no fields, so there are no bindings.

Option and Result#

Option[T] has Some(value) and None. Result[T, E] has Success(value) and Error(reason).

fn report(result Result[i32, str]) {
    match result {
        Success(value) => print($"ok: {value}")
        Error(reason) => print($"error: {reason}")
    }
}

fn report_optional(value Option[i32]) {
    match value {
        Some(v) => print(v)
        None => print("none")
    }
}

Use ? to propagate an Error and match to handle or transform it locally.

The is Operator#

is tests a value against a pattern and produces a boolean. Its pattern syntax matches an arm.

const value Option[i32] = Some(3)

if value is Some(v) {
    print(v)
}

if value is None {
    print("empty")
}

In an if condition, bindings are visible in the then branch only.

if value is Success(v) {
    print(v)
} else {
    print("failed")
}

A refinement type may be used as a pattern. The pattern checks the invariant and binds a value of the refinement type when it holds.

type Positive = i32 : invariant self > 0

fn main() {
    const raw = 42
    if raw is Positive(p) {
        print(p)
    } else {
        print("not positive")
    }
}

Refinement patterns are only available through is. match requires a sum type, enum, Option, or Result.

Narrowing#

An is test introduces a new immutable binding; the original variable is unchanged.

fn take_positive(n i32) i32 {
    if n is Positive(p) {
        return p * 2
    }
    return 0
}

A binding from a pattern is not visible outside its branch. Declare a variable before the branch if the value is needed after it.

fn positive_or_zero(n i32) i32 {
    var result = 0
    if n is Positive(p) {
        result = p
    }
    return result
}

Nested Decisions#

Patterns bind names; they do not nest. Bind the outer field and match it in a second step.

fn depth(value Option[Tree]) i32 => match value {
    Some(tree) => match tree {
        Leaf => 0
        Node(left, _, right) => 1 + depth(Some(left)) + depth(Some(right))
    }
    None => 0
}

There are no guards or literal patterns. Express a guard with an if inside the arm.

Match as an Expression#

const label = match value {
    Some(v) => $"value {v}"
    None => "missing"
}

Arm bodies must have compatible types. A void arm may not be mixed with a value-producing arm.

Style#

List every variant when the type is closed and stable. Use a wildcard arm with a deliberate default when the type is likely to grow. Prefer is for a single case and match for two or more. Keep arm bodies short.