> ## 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 While Let

The `while let` construct is a control flow operator that combines a `while` loop with pattern matching. It repeatedly evaluates an expression, attempts to bind the result to a specified pattern, and executes the loop body strictly as long as the pattern match succeeds. The loop terminates immediately upon the first matching failure.

```rust theme={"dark"}
fn main() {
    let mut iterator = vec![1, 2, 3].into_iter();
    
    // Loop body executes as long as `iterator.next()` matches the `Some(value)` pattern
    while let Some(value) = iterator.next() {
        println!("Matched: {}", value);
    }
}
```

## Mechanics and Behavior

* **Evaluation:** The expression on the right side of the `=` is evaluated at the beginning of every iteration.
* **Binding and Scope:** If the evaluated expression matches the pattern on the left side of the `=`, any variables declared within the pattern are bound to the corresponding inner values. The scope of these bound variables is restricted entirely to the loop body.
* **Termination:** If the expression evaluates to a variant that does not match the pattern (e.g., matching a `Some(T)` pattern against a `None` value), the binding fails, the loop terminates, and control flow proceeds to the statement immediately following the loop block.
* **Non-Exhaustiveness:** Unlike a standard `match` expression, `while let` does not require exhaustive pattern matching. It implicitly handles all non-matching variants by breaking the loop, bypassing the need for a catch-all `_` arm.

## Desugared Equivalent

Internally, `while let` is syntactic sugar for an infinite `loop` containing a `match` expression that explicitly breaks on non-matching variants.

```rust theme={"dark"}
fn main() {
    let mut stream_one = vec![10].into_iter();
    
    // The while let construct:
    while let Some(value) = stream_one.next() {
        println!("while let: {}", value);
    }

    let mut stream_two = vec![20].into_iter();
    
    // Is structurally equivalent to:
    loop {
        match stream_two.next() {
            Some(value) => {
                println!("loop match: {}", value);
            }
            _ => break, // Implicit termination on match failure
        }
    }
}
```

## Multiple Patterns

The `while let` construct supports matching against multiple patterns simultaneously using the pattern alternation operator (`|`), also known as an or-pattern. The loop continues as long as the expression matches *any* of the specified patterns. Any variables bound in the pattern must be bound in all alternatives.

```rust theme={"dark"}
enum Token {
    Integer(i32),
    Float(i32), // Using i32 for demonstration simplicity
    Eof,
}

fn main() {
    let mut tokens = vec![Token::Integer(1), Token::Float(2), Token::Eof, Token::Integer(3)];
    
    // Pops from the end: Integer(3), Eof, Float(2), Integer(1)
    // The loop terminates when it encounters Token::Eof
    while let Some(Token::Integer(val) | Token::Float(val)) = tokens.pop() {
        println!("Extracted numeric value: {}", val);
    }
}
```

## Let Chains (Unstable / Nightly)

On nightly Rust toolchains, `while let` supports `let` chains via the unstable `#![feature(let_chains)]` flag. This feature allows combining multiple pattern matches and standard boolean conditions within a single `while` condition using the logical AND operator (`&&`).

The loop body executes only if all chained conditions and pattern matches succeed sequentially. If any part of the chain fails, the evaluation short-circuits and the loop terminates. Variables bound in earlier `let` expressions within the chain are immediately in scope for subsequent conditions in the same chain, as well as within the loop body.

```rust theme={"dark"}
#![feature(let_chains)]

fn main() {
    let mut values = vec![1, 2, 3];
    let mut condition = true;

    // The loop requires:
    // 1. `values.pop()` to match `Some(val)`
    // 2. `condition` to evaluate to `true`
    // 3. `Some(val * 2)` to match `Some(doubled)`
    while let Some(val) = values.pop() && condition && let Some(doubled) = Some(val * 2) {
        println!("Processed: {}", doubled);
        
        if val == 2 {
            condition = false; // Will cause the chain to fail on the next iteration
        }
    }
}
```

<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>
