expression
Rust Exit a labeled block early with a value
A labeled block can finish early with a result without repeating like a loop. Use it for a local calculation with an early exit. Expected output (not executed): 0 Why: The labeled break skips the normal calculation and supplies zero. Common mistake: A break targeting a block must name its label.
Formal Syntax & Structure
rust syntaxrust
'label: { ... break 'label value; ... }Working Example
Example Snippetrust
fn main() { let quantity = 0; let total = 'cost: { if quantity == 0 { break 'cost 0; } quantity * 5 + 2 }; println!("{total}");}