Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Patterns

Or-patterns and match guards

Reference for Rust or-patterns (a | b) and match guards (if condition): sharing one arm between alternatives, nested or-patterns, guard evaluation order and exhaustiveness limits.

An or-pattern joins alternatives with |: the arm matches when any alternative matches. A match guard is an extra boolean condition written after the pattern with if; the arm is taken only when the pattern matches and the guard is true.

They are independent tools that are often combined. Or-patterns can be nested inside other patterns (Some(1 | 2)) and are allowed in let, if let and function parameters, not just match.

Rust
match n {
    1 | 2 | 3 => "small",
    x if x % 2 == 0 => "even",
    _ => "other",
}

Try it

Rust Alternatives and guards

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Guards run in order and apply to all alternatives

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
pattern1 | pattern2 | pattern3 => expr,
pattern if condition => expr,
pattern1 | pattern2 if condition => expr,
Some(a | b)
(x @ (1 | 2), y)
Part Description
| in patterns Each alternative must bind the same set of names with the same types. A leading | is allowed for formatting.
if condition Any bool expression. It can use the bindings made by the pattern.

Errors and edge cases: if the alternatives bind different names, the compiler reports E0408. The compiler does not reason about guards for exhaustiveness: a match whose arms all have guards still needs an unguarded final arm, or it fails with E0004. Inside a guard the bindings can only be read, so you cannot move out of them there.

Usage notes

  • Order matters. Arms are tried top to bottom, and a guard is evaluated only after its pattern matched. Put specific arms before general ones.
  • A guard applies to the whole arm. 4 | 5 if flag means (4 | 5) if flag, not "4, or 5 if flag". If the guard fails, matching continues with the next arm. When several alternatives could match the same value with different bindings, such as (x, 1) | (1, x) if cond, the guard runs once per matching alternative until one passes.
  • Guards can have side effects, which is legal but confusing. Keep them pure and cheap.
  • Match ergonomics inside guards. If you match &Option<String>, a guard sees &String: Some(s) if s.is_empty().
  • Prefer patterns to guards when you can. Some(0..=9) states the same as Some(n) if n < 10 and lets the compiler check exhaustiveness.
  • Or-patterns in let and parameters are allowed only when irrefutable, for example let (Ok(x) | Err(x)) = res; when both variants carry the same type.