> ## 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 Break with Value

In Rust, the `break` keyword can terminate execution of a `loop` or a labeled block expression and simultaneously yield a value to the surrounding scope. Because these constructs are expressions, they evaluate to a value, and `break` serves as the mechanism to return that value to the binding context.

## Syntax

To return a value, append the expression directly after the `break` keyword. If breaking from a specific labeled construct, the label must precede the value expression.

When assigning the result of a `loop` or labeled block to a variable via a `let` statement, the `let` statement itself must be terminated with a semicolon. However, if the `loop` or labeled block is used as a trailing expression (e.g., as the implicit return value of a function), no semicolon is required.

```text theme={"dark"}
// Standard loop syntax
let binding = loop {
    break expression;
};

// Labeled block syntax (Rust 1.65+)
let binding = 'label: {
    break 'label expression;
};
```

## Mechanics and Compiler Rules

* **Type Inference:** The type of the `loop` or labeled block expression is inferred from the type of the expression passed to `break`.
* **Type Homogeneity:** If a single construct contains multiple `break` statements, every `break` must yield a value of the exact same type.
* **Construct Restriction:** Yielding a value via `break` is supported by the `loop` keyword and labeled blocks. It is a compilation error to attempt to return a value using `break` inside `while`, `while let`, or `for` loops. Those constructs inherently evaluate to the unit type `()`.
* **Labeled Block Requirement:** Breaking out of a labeled block *always requires* the label to be explicitly specified (e.g., `break 'block_name value;`). In contrast, breaking from an unnested `loop` does not require a label (e.g., `break value;`).
* **Unit Type Default:** If a construct is broken using `break;` without an expression, it implicitly yields the unit type `()`.

## Code Examples

**Basic Value Yielding**
In this example, the `loop` evaluates to an `i32`, which is bound to `result`.

```rust theme={"dark"}
let mut counter = 0;

let result = loop {
    counter += 1;

    if counter == 10 {
        break counter * 2; 
    }
};

// result is 20
```

**Combining with Loop Labels**
When dealing with nested loops, `break` can be combined with a loop label to yield a value from an inner loop directly to the assignment of an outer loop.

```rust theme={"dark"}
let mut x = 0;

let result = 'outer: loop {
    x += 1;
    let mut y = 0;
    
    loop {
        y += 1;
        
        if x == 2 && y == 3 {
            // Terminates the 'outer loop and yields the value 50
            break 'outer 50; 
        }
        
        if y == 5 {
            // Terminates the inner loop, allowing 'outer to iterate again
            break; 
        }
    }
};

// result is 50
```

**Yielding from a Labeled Block**
Since Rust 1.65, `break` can yield a value from a standard block expression annotated with a label, bypassing the need for a `loop` construct. The label is mandatory when breaking from a block.

```rust theme={"dark"}
let condition = true;

let result = 'block: {
    if condition {
        break 'block 100;
    }
    
    // Default block evaluation if break is not reached
    200 
};

// result is 100
```

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