Continue and nested loops
Quick answer
continue skips the remaining statements in the current iteration and proceeds to the next iteration. A nested loop runs inside another loop. Labels let break or continue target a particular enclosing loop instead of the default innermost one.
Skip versus exit
Use continue to ignore an item without ending the entire loop. Use break to end a loop. A loop label starts with an apostrophe and is followed by a colon at its declaration, for example 'rows:. A labeled break leaves the named loop, including any inner loops.
fn main() { for n in 1..=4 { if n == 2 { continue; } println!("{n}"); }}Expected stdout (not executed):
134Only the iteration for two skips printing.
fn main() { 'rows: for row in 1..=3 { for col in 1..=2 { println!("{row},{col}"); if row == 2 && col == 1 { break 'rows; } } }}Expected stdout (not executed):
1,11,22,1When row two, column one is reached, both loops end.
Common mistake
An unlabeled break exits only the inner loop. In a while loop, continue can accidentally skip a required counter update and prevent progress.
Try it yourself
Visit a two-by-three grid, skipping column two. Predict the printed pairs before running the code locally.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Continue and nested loops — practice quiz
Choose one answer per question, then review the explanation.
A two-row, three-column nested loop prints every pair except column 2, using continue in the inner loop. How many pairs print?
An unlabeled break runs inside an inner loop. Which loop ends?
Given an outer loop labeled `'rows:`, which statement exits it from inside another loop?