Skip to content

Nested Patterns

Any sub-pattern position accepts another full pattern. This allows extracting data from arbitrarily deep structures — enum inside array, struct containing enum, enum inside enum — without nesting multiple match expressions or using temporary variables.

Enum inside array; struct containing enum; enum inside enum.

12-nested-patterns.zolo
Playground
// Feature: Nested patterns — combining everything
// Syntax: subpatterns inside subpatterns
// When to use: extract data from deep structures in a single pass
// without nesting `match` or `if let`.

enum Maybe<T> {
  Just(T),
  Nothing,
}

// Enum INSIDE array.
let opts = [Maybe.Just(1), Maybe.Nothing, Maybe.Just(3)]

for o in opts {
  let label = match o {
    Maybe::Just(n) => "got {n}",
    Maybe::Nothing => "none",
  }
  print(label)
}

// Struct containing an enum.
struct Box {
  label: str,
  content: Maybe<int>,
}

let b1 = Box { label: "alpha", content: Maybe.Just(10) }
let b2 = Box { label: "beta", content: Maybe.Nothing }

fn describe_box(b: Box) -> str {
  return match b {
    Box { label, content: Maybe::Just(n) } => "{label} = {n}",
    Box { label, content: Maybe::Nothing } => "{label} = empty",
  }
}

print(describe_box(b1))  // alpha = 10
print(describe_box(b2))  // beta = empty

// Enum nested in enum.
enum Result2 {
  Ok(Maybe<int>),
  Err(str),
}

let r1 = Result2.Ok(Maybe.Just(42))
let r2 = Result2.Ok(Maybe.Nothing)
let r3 = Result2.Err("failed")

fn classify(r: Result2) -> str {
  return match r {
    Result2::Ok(Maybe::Just(n)) => "ok with {n}",
    Result2::Ok(Maybe::Nothing) => "ok but empty",
    Result2::Err(msg) => "error: {msg}",
  }
}

print(classify(r1))  // ok with 42
print(classify(r2))  // ok but empty
print(classify(r3))  // error: failed

The example above shows three forms of nesting:

  1. Enum in iteration — the match inside the for destructures each array element directly.
  2. Struct with enum field — the pattern Box { label, content: Maybe::Just(n) } matches the content field against an enum sub-pattern, extracting label and n in a single arm.
  3. Enum in enumResult2::Ok(Maybe::Just(n)) traverses two levels of variant in one line.

The general rule is: wherever a pattern expects a value, you can place another, more specific pattern.

enespt-br