> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust Or Pattern

An Or pattern in Rust is a structural matching construct denoted by the pipe operator (`|`) that allows a single pattern-matching arm to evaluate against multiple alternative patterns. If the scrutinized value matches any of the specified alternatives, the overall pattern match succeeds.

```rust theme={"dark"}
pattern_1 | pattern_2 | ... | pattern_n => expression,
```

## Core Mechanics and Evaluation

Or patterns evaluate strictly from left to right. The compiler utilizes short-circuit evaluation; the first alternative that successfully matches the scrutinized value terminates the pattern evaluation, and the associated code block is executed.

While Or patterns are valid in any pattern-matching context (including `match`, `if let`, and `while let`), their use in `let` statements and function parameters carries a strict irrefutability requirement. Because `let` bindings and function parameters cannot handle match failures, the Or pattern used in these contexts must be *irrefutable* (guaranteed to match all possible values of the type). Most Or patterns, such as matching specific integers, are refutable and will cause a compile-time error in these contexts.

```rust theme={"dark"}
let x = 2;

// Refutable context: match expression
match x {
    1 | 2 | 3 => println!("Matched 1, 2, or 3"),
    _ => println!("Matched something else"),
}

// Refutable context: if let expression
if let 4 | 5 = x {
    println!("Matched 4 or 5");
}

// Irrefutable context: let statement
// VALID: The Or pattern exhaustively covers both variants of Result<i32, i32>
let Ok(val) | Err(val) = Result::<i32, i32>::Ok(10);

// INVALID: Refutable pattern in a let statement causes a compile-time error
// let 1 | 2 = x; 
```

## Variable Binding Uniformity

The most strict compiler constraint regarding Or patterns is **binding uniformity**. If an Or pattern binds variables to extract data from the scrutinized value, every alternative within that Or pattern must bind the exact same set of variables.

Furthermore, the bound variables must resolve to the exact same type *and* utilize the exact same binding mode (`mut`, `ref`, `ref mut`, or by value) across all alternatives.

```rust theme={"dark"}
enum Node {
    Leaf(i32),
    Branch(i32, i32),
}

let node = Node::Leaf(5);

match node {
    // VALID: `val` is bound in both alternatives, is of type `i32` in both, 
    // and is bound by value in both.
    Node::Leaf(val) | Node::Branch(val, _) => println!("Value: {}", val),
}
```

Violating these constraints results in specific compile-time errors:

* Failing to bind the same variables yields `E0408: variable is not bound in all patterns`.
* Using different binding modes yields `E0409: variable is bound inconsistently`.

```rust theme={"dark"}
let opt = Some(10);

match opt {
    // INVALID (E0408): `x` is bound in the first alternative, but not the second.
    // Some(x) | None => println!("Value"), 

    // INVALID (E0409): `x` is bound by value in the first alternative, 
    // but by `mut` in the second alternative.
    // Some(x) | Some(mut x) => println!("Value"),

    _ => ()
}
```

## Nested Or Patterns

Or patterns can be nested arbitrarily deep within other structural patterns (such as tuples, slices, structs, or enum variants). You do not need to duplicate the outer structure to match against multiple inner values.

```rust theme={"dark"}
let result: Result<Option<i32>, &str> = Ok(Some(42));

match result {
    // The Or pattern (1 | 2 | 42) is nested inside the Option, 
    // which is nested inside the Result.
    Ok(Some(1 | 2 | 42)) => println!("Matched specific inner values"),
    Ok(None) | Err(_) => println!("Matched other states"),
    _ => ()
}
```

## Precedence and Grouping

The `|` operator has low precedence in pattern syntax. When combining Or patterns with other pattern features, such as range patterns (`..=`) or reference modifiers (`&`, `ref`), the Or pattern applies to the entire pattern on either side of the pipe.

Parentheses must be used to explicitly group Or patterns to control precedence and restrict the scope of the alternation.

```rust theme={"dark"}
let reference = &1;

match reference {
    // VALID: Parentheses group the Or pattern. 
    // This matches a reference to `0` OR a reference to `1`.
    &(0 | 1) => println!("Matched a reference to 0 or 1"),
    
    // INVALID PRECEDENCE: Without parentheses, `&0 | 1` is parsed as:
    // (&0) | (1)
    // This attempts to match either a reference to 0, or the integer 1.
    // This causes a type mismatch error because the alternatives have different types.
    // &0 | 1 => println!("Will not compile"),
    
    _ => ()
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
