docs / Getting Started / Pattern Matching

Pattern Matching

Pattern matching is a core feature of Miri. It's exhaustive — the compiler checks that all cases are covered.

enum Direction
    North
    South
    East
    West

fn direction_name(d Direction) String
    match d
        Direction.North: "north"
        Direction.South: "south"
        Direction.East: "east"
        Direction.West: "west"

Destructuring

enum Shape
    Circle(int)
    Rectangle(int)

fn area(s Shape) int
    match s
        Shape.Circle(r): r * r
        Shape.Rectangle(side): side * side

Matching on values

fn describe_number(n int) string
    match n
        0
            return "zero"
        1
            return "one"
        _
            return "something else"

Match supports or-patterns (2 | 3), guard clauses (x if x > 10), and wildcard (_).