0
0
Rustprogramming~10 mins

Loop labels in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Loop labels
Start Outer Loop 'outer'
Start Inner Loop 'inner'
Check Condition in Inner
Break 'outer'
Exit Outer
Repeat Outer
The flow shows how labeled loops let you break or continue specific loops, even nested ones.
Execution Sample
Rust
fn main() {
    'outer: for i in 1..=3 {
        'inner: for j in 1..=3 {
            if i == 2 && j == 2 {
                break 'outer;
            }
            println!("i={}, j={}", i, j);
        }
    }
}
This code uses loop labels to break the outer loop from inside the inner loop when i and j both equal 2.
Execution Table
StepijCondition (i==2 && j==2)ActionOutput
111falsePrint i=1, j=1i=1, j=1
212falsePrint i=1, j=2i=1, j=2
313falsePrint i=1, j=3i=1, j=3
421falsePrint i=2, j=1i=2, j=1
522trueBreak 'outer' loop
6---Exit all loops
💡 At step 5, condition is true, so break 'outer' exits all loops.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
i-11122-
j-12312-
Key Moments - 3 Insights
Why does the program stop printing after i=2, j=2?
Because at step 5 in the execution_table, the condition i==2 && j==2 is true, and break 'outer' exits the outer loop immediately, stopping all loops.
What happens if we use break without a label inside nested loops?
Without a label, break only exits the innermost loop. Here, break 'outer' is needed to exit the outer loop from inside the inner loop, as shown in step 5.
Can continue use labels like break?
Yes, continue can also use labels to skip to the next iteration of a specific loop, but this example only shows break with labels.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of j at step 4?
A1
B2
C3
D-
💡 Hint
Check the 'j' column at step 4 in the execution_table.
At which step does the program break out of the outer loop?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look at the 'Action' column where break 'outer' is executed.
If we remove the label 'outer' from break, what would happen?
ABreaks the outer loop anyway
BCauses a compile error
CBreaks only the inner loop
DContinues the inner loop
💡 Hint
Recall that break without label exits only the innermost loop, as explained in key_moments.
Concept Snapshot
Loop labels in Rust let you name loops.
Use 'label: for ...' to name a loop.
Use break 'label or continue 'label to control specific loops.
This helps manage nested loops clearly.
Without labels, break/continue affect only innermost loop.
Full Transcript
This example shows how Rust loop labels work. We have two loops: outer and inner. The inner loop runs inside the outer loop. When i equals 2 and j equals 2, the program breaks the outer loop using break 'outer;. This stops all looping immediately. The execution table shows each step: printing values until the break condition is met. Variable tracker shows how i and j change. Key moments explain why the program stops and how labels affect break behavior. The quiz tests understanding of variable values and loop control. Loop labels help control nested loops precisely.