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

The `break` keyword in Rust is a control flow expression used to immediately terminate the execution of the innermost enclosing `loop`, `while`, `for` construct, or a targeted labeled block expression. When evaluated, control is unconditionally transferred to the statement immediately following the terminated construct.

## Basic Loop Syntax

By default, an unannotated `break` halts the current iteration and exits the closest enclosing loop.

```rust theme={"dark"}
loop {
    break; // Execution jumps to the end of this loop block
}
```

## Labeled Block Expressions

Since Rust 1.65, `break` can be used to terminate regular block expressions that are annotated with a label. Unlike loops, breaking out of a non-loop block strictly requires the label to be specified in the `break` statement.

```rust theme={"dark"}
'block: {
    break 'block; // Execution jumps to the end of this labeled block
}
```

## Returning Values via `break`

In Rust, `loop` constructs and labeled block expressions can evaluate to a value. The `break` statement accepts an optional expression payload to return a value from these constructs.

*Note: This mechanic is limited to `loop` and labeled blocks. The `while` and `for` constructs cannot return values via `break` and always evaluate to the unit type `()`.*

```rust theme={"dark"}
let loop_result: i32 = loop {
    break 42; // Terminates the loop and yields '42'
};

let block_result: i32 = 'block: {
    break 'block 99; // Terminates the block and yields '99'
};
```

## Control Flow Labels

Rust supports control flow labels, denoted by a single quote followed by an identifier (`'label`). Labels allow `break` to bypass the default behavior of targeting the innermost loop, enabling the termination of specific outer loops within a nested structure.

```rust theme={"dark"}
'outer: for i in 0..10 {
    'inner: loop {
        break 'outer; // Terminates the 'outer for loop, bypassing 'inner
    }
}
```

## Combining Labels and Values

When working with nested `loop` or labeled block expressions, `break` can simultaneously target a specific label and return a value to that construct's binding. The syntax requires the label to precede the value expression.

```rust theme={"dark"}
let nested_result = 'outer: loop {
    loop {
        break 'outer 100; // Terminates 'outer and yields 100 to 'nested_result'
    }
};
```

## Type System Implications

* **Expression Type:** The `break` expression itself represents a divergence in control flow. Therefore, it evaluates to the never type (`!`). This allows `break` to be used in contexts that expect a specific type, as `!` can be coerced into any type.
* **Construct Type:** If a `loop` or labeled block contains multiple `break` statements that return values, the compiler enforces that all yielded expressions must resolve to the same type. If `break` is used without a payload, the construct evaluates to the unit type `()`.

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