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

A labeled loop in Rust is a control flow mechanism that assigns a unique identifier (a label) to a loop construct, allowing `break` and `continue` statements to explicitly target a specific loop within nested loop hierarchies. By default, `break` and `continue` operate on the innermost enclosing loop; labels override this lexical scoping behavior to transfer control to a higher-level loop.

Labels are defined using a single quote (`'`) followed by a valid Rust identifier and a colon (`:`). They are placed immediately preceding the loop keyword.

## Syntax

```rust theme={"dark"}
'label_name: loop {
    // Control flow targeting the label
    if true {
        continue 'label_name;
    }
    break 'label_name;
}
```

Labels can be applied to all Rust loop constructs: `loop`, `while`, `while let`, and `for`.

## Control Flow Mechanics

When a label is invoked by a control flow statement, the compiler bypasses the innermost loop context and resolves the instruction against the AST node of the labeled loop:

* **`break 'label;`**: Immediately terminates the execution of the loop associated with `'label`. Control flow resumes at the first statement following the labeled loop's closing brace.
* **`continue 'label;`**: Immediately halts the current iteration of the innermost loop and transfers control to the next iteration of the loop associated with `'label`. For `for` loops, this advances the iterator; for `while` loops, this re-evaluates the condition.

```rust theme={"dark"}
'outer: for i in 1..=3 {
    'inner: for j in 1..=3 {
        if i == 1 && j == 2 {
            continue 'outer; // Skips the rest of 'inner, advances 'outer to i = 2
        }
        if i == 2 && j == 3 {
            break 'outer; // Terminates 'outer completely
        }
    }
}
```

## Returning Values with Labels

In Rust, the unconditional `loop` construct is an expression that can return a value via the `break` statement. When using labeled loops, the return expression is placed immediately after the label identifier.

*Note: Value return via `break` is strictly limited to `loop` constructs; `for` and `while` loops evaluate to the unit type `()` and cannot return values.*

```rust theme={"dark"}
let computed_value = 'evaluation: loop {
    let mut internal_state = 0;
    
    loop {
        internal_state += 1;
        if internal_state > 10 {
            // Terminates 'evaluation and binds 42 to computed_value
            break 'evaluation 42; 
        }
    }
};
```

## Scope and Shadowing

Loop labels and lifetimes share the same namespace in Rust, which is distinct from the namespaces used for variables and types. They follow lexical scoping rules. If an inner loop defines a label with the exact same identifier as an outer loop, the inner label shadows the outer one. Any `break` or `continue` referencing that identifier within the inner loop's scope will resolve to the inner loop. The Rust compiler will emit a warning for label shadowing to prevent unintended control flow resolution.

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