Loop Labels

This lesson discusses loop labels in Rust.

What Is a Loop Label?

A loop label assigns an identifier to a loop.

Syntax

Write a label and colon before the loop.

Example

The code below prints the multiplication table of 1 to 5 except 3. See how specifying a loop label helps you to skip the table of 3.

Press + to interact
fn main() {
'outer:for i in 1..5 { //outer loop
println!("Muliplication Table : {}", i);
'inner:for j in 1..5 { // inner loop
if i == 3 { continue 'outer; } // Continues the loop over `i`.
if j == 2 { continue 'inner; } // Continues the loop over `j`.
println!("{} * {} = {}", i, j, i * j);
}
}
}

Explanation

...