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

The `loop` keyword in Rust is a control flow construct that creates an unconditional, infinite loop. It continuously executes the associated block of code until an explicit termination instruction is encountered. Because it lacks a conditional evaluation phase, the compiler treats `loop` as guaranteeing execution, which directly impacts control flow analysis, type inference, and definite initialization checks.

## Syntax and Control Flow

The execution flow within a `loop` is manipulated using two primary keywords:

* `break`: Immediately halts execution of the loop and transfers control to the statement following the loop block.
* `continue`: Bypasses any remaining statements in the current iteration and immediately jumps back to the beginning of the loop block.

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

loop {
    counter += 1;
    
    if counter == 1 {
        continue; // Skips the rest of this iteration
    }
    
    if counter == 2 {
        break; // Terminates the loop entirely
    }
}
```

## Expression Semantics and Yielding Values

In Rust, `loop` is an expression rather than a statement. This means a `loop` can evaluate to a value and bind that value to a variable. Values are yielded out of the loop by appending the desired expression directly after the `break` keyword.

```rust theme={"dark"}
let _value: i32 = loop {
    // Yields 42, terminates the loop, and binds to '_value'
    break 42; 
};
```

If a `loop` does not explicitly yield a value via `break`, its evaluation type is the unit type `()`, provided it eventually breaks. If the loop never breaks, its type evaluates to the never type (`!`).

## Loop Labels

When dealing with nested loops, `break` and `continue` apply to the innermost loop by default. Rust utilizes loop labels—identifiers prefixed with a single quote (`'`)—to explicitly target specific outer loops. A label is declared immediately before the `loop` keyword, followed by a colon.

```rust theme={"dark"}
'outer_label: loop {
    loop {
        // Terminates the outer loop, bypassing the inner loop's default behavior
        break 'outer_label; 
    }
}
```

Labels can be combined with value yielding in a single `break` statement. The label must precede the yielded value.

```rust theme={"dark"}
let _result = 'outer: loop {
    loop {
        // Breaks the loop labeled 'outer' and yields the value 100
        break 'outer 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>
