# Pattern matching

`match` is **exhaustive**: missing a variant is a compile error (**E0340**). That is what makes it safe to add a variant later, since the compiler points you at every place that must handle it.

```cplus
fn describe(s: Shape) -> i32 {
    return match s {
        Shape::Circle(r)         => (r as i32) *% 2,
        Shape::Rectangle(w, h)   => (w as i32) *% (h as i32),
        Shape::Square(side)      => (side as i32),
    };
}
```

Add a catch-all `_` arm when you genuinely do not care about the rest:

```cplus
return match c {
    Color::Red => 1,
    _          => 0,
};
```

## `if let`: extract on the happy path

```cplus
if let Maybe[i32]::Some(v) = m {
    println(v);
}
```

## `guard let`: match or diverge

`guard let` binds the value and forces the failure case to leave the scope, so the rest of the function reads straight through:

```cplus
fn process(m: Maybe[i32]) -> i32 {
    guard let Maybe[i32]::Some(v) = m else { return 0 -% 1; };
    return v +% 1;          // `v` is in scope after the guard
}
```

The `else` block must **diverge** via `return`, `break`, `continue`, or `loop`. The compiler enforces that.

## `while let`

```cplus
while let Maybe[i32]::Some(v) = next() {
    println(v);
}
```

These binding forms, together with the control-flow primitives, are how C+ does [error handling](/docs/error-handling) without exceptions or a `?` operator.
