Bird
Raised Fist0
Rustprogramming~10 mins

Loop labels in Rust - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using loop labels in Rust?
easy
A. To create infinite loops without conditions.
B. To declare variables inside loops.
C. To name loops so you can break or continue specific loops inside nested loops.
D. To automatically parallelize loops for performance.

Solution

  1. Step 1: Understand loop labels concept

    Loop labels let you assign a name to a loop, especially useful when loops are nested.
  2. Step 2: Identify the use of labels

    Labels allow you to break or continue a specific loop by referring to its name, avoiding ambiguity.
  3. Final Answer:

    To name loops so you can break or continue specific loops inside nested loops. -> Option C
  4. Quick Check:

    Loop labels = control nested loops [OK]
Hint: Labels name loops for clear break/continue control [OK]
Common Mistakes:
  • Thinking labels create infinite loops
  • Confusing labels with variable declarations
  • Assuming labels optimize performance automatically
2. Which of the following is the correct syntax to label a loop in Rust?
easy
A. label: loop { /* code */ }
B. loop label { /* code */ }
C. loop: label { /* code */ }
D. loop { label: /* code */ }

Solution

  1. Step 1: Recall Rust loop label syntax

    In Rust, a loop label is written as 'label: before the loop keyword.
  2. Step 2: Match syntax options

    label: loop { /* code */ } correctly places the label before the loop with a colon, e.g., 'label: loop {}.
  3. Final Answer:

    label: loop { /* code */ } -> Option A
  4. Quick Check:

    Label syntax = 'label: loop' [OK]
Hint: Labels come before loop with a colon [OK]
Common Mistakes:
  • Placing label after loop keyword
  • Using colon after loop instead of label
  • Putting label inside loop body
3. What will be the output of this Rust code?
fn main() {
    let mut count = 0;
    'outer: loop {
        println!("Outer loop: {}", count);
        let mut inner_count = 10;
        loop {
            println!("  Inner loop: {}", inner_count);
            if inner_count == 9 {
                break 'outer;
            }
            inner_count -= 1;
        }
        count += 1;
    }
    println!("Done");
}
medium
A. Outer loop: 0 Inner loop: 10 Inner loop: 9 Inner loop: 8 Done
B. Outer loop: 0 Inner loop: 10 Done
C. Infinite loop with no output
D. Outer loop: 0 Inner loop: 10 Inner loop: 9 Done

Solution

  1. Step 1: Trace outer loop first iteration

    count starts at 0, prints "Outer loop: 0". Then inner loop starts with inner_count = 10.
  2. Step 2: Trace inner loop and break condition

    Inner loop prints 10, then 9. When inner_count == 9, it breaks the 'outer loop, exiting both loops.
  3. Final Answer:

    Outer loop: 0 Inner loop: 10 Inner loop: 9 Done -> Option D
  4. Quick Check:

    Break 'outer stops both loops at inner_count 9 [OK]
Hint: Break 'label exits named loop immediately [OK]
Common Mistakes:
  • Thinking break only exits inner loop
  • Missing that break 'outer exits both loops
  • Assuming loops continue infinitely
4. Identify the error in this Rust code using loop labels:
fn main() {
    'start: loop {
        loop {
            break start;
        }
    }
}
medium
A. Missing apostrophe before label name in break statement.
B. Label 'start' is not defined before use.
C. Cannot break a labeled loop from an inner loop.
D. Syntax error: break statements cannot have labels.

Solution

  1. Step 1: Check break statement syntax

    In Rust, breaking a labeled loop requires the label with an apostrophe, e.g., break 'start;.
  2. Step 2: Identify the missing apostrophe

    The code uses break start; missing the apostrophe before the label name, causing a syntax error.
  3. Final Answer:

    Missing apostrophe before label name in break statement. -> Option A
  4. Quick Check:

    Label break syntax = break 'label [OK]
Hint: Always prefix label with apostrophe in break/continue [OK]
Common Mistakes:
  • Omitting apostrophe in break label
  • Assuming labels are variables
  • Thinking break can't target outer loops
5. Given nested loops labeled 'outer and 'inner, how can you skip the current iteration of the outer loop from inside the inner loop?
hard
A. Use break 'inner; inside the inner loop.
B. Use continue 'outer; inside the inner loop.
C. Use continue; without label inside the inner loop.
D. Use break 'outer; inside the inner loop.

Solution

  1. Step 1: Understand continue with labels

    Using continue 'outer; skips the current iteration of the loop labeled 'outer, jumping to its next iteration.
  2. Step 2: Differentiate break and continue

    break exits the loop, while continue skips to the next iteration. To skip outer loop iteration from inner loop, use continue 'outer;.
  3. Final Answer:

    Use continue 'outer; inside the inner loop. -> Option B
  4. Quick Check:

    continue 'label skips that loop iteration [OK]
Hint: Use continue 'label to skip outer loop iteration [OK]
Common Mistakes:
  • Using break instead of continue to skip iteration
  • Omitting label in continue statement
  • Confusing break and continue effects
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard