Rust Break and Continue
break and continue let you interrupt a loop early instead of always running it to the end. They work the same way in all three kinds of loop: loop, while and for.
break — exit the loop entirely
Section titled “break — exit the loop entirely”break stops the loop immediately, and execution jumps to whatever comes right after it:
fn main() { for i in 1..=10 { if i == 5 { break; } println!("{i}"); }}Output:
1234Even though the range goes up to 10, the loop stops the moment i hits 5.
continue — skip to the next round
Section titled “continue — skip to the next round”continue skips the rest of the current pass and jumps straight to the next one, rather than stopping the loop altogether:
fn main() { for i in 1..=10 { if i % 2 == 0 { continue; // skip even numbers } println!("{i}"); }}Output:
13579Every even number gets skipped — continue fires, and the loop moves straight to the next value of i without running println!.
Labeled loops
Section titled “Labeled loops”When you nest one loop inside another, plain break/continue only affects the innermost loop. If you need to break out of an outer loop from inside a nested one, give the outer loop a label (written as 'name:):
fn main() { 'outer: for x in 1..=3 { for y in 1..=3 { if x * y > 4 { break 'outer; } println!("{x} x {y} = {}", x * y); } }}Output:
1 x 1 = 11 x 2 = 21 x 3 = 32 x 1 = 22 x 2 = 4Without the label, break would only stop the inner y loop, and x would keep going. break 'outer; stops both at once. continue 'outer; works the same way, if you want to skip to the outer loop’s next round instead of stopping entirely.
The continue trap in while loops
Section titled “The continue trap in while loops”In a for loop, the next value comes from the range automatically, so continue is always safe. A while loop is different: you update the counter, usually at the bottom of the body. If continue jumps over that update, the counter never changes and the loop runs forever.
This version tries to skip 3, but it hangs. Don’t run it:
fn main() { let mut i = 0; while i < 5 { if i == 3 { continue; // ❌ jumps back up without changing i } println!("{i}"); i += 1; }}When i is 3, continue goes back to the condition. i is still 3, so continue fires again, and again, forever. The fix is to update the counter before anything that might continue:
fn main() { let mut i = 0; while i < 5 { i += 1; // update first, so continue can't skip it if i == 3 { continue; } println!("{i}"); }}Output:
1245Moving i += 1 to the top shifts the numbers by one: this loop prints 1 to 5 instead of 0 to 4, so adjust the start or the condition if you need the old range. Even better, if you’re just counting, use a for loop over a range and the problem can’t happen.
Practice
Section titled “Practice”1. Skip the multiples of 3
Section titled “1. Skip the multiples of 3”Print the numbers from 1 to 10, but skip every multiple of 3. Hint: a number is a multiple of 3 when n % 3 == 0. Use continue to skip it.
Show solution
fn main() { for n in 1..=10 { if n % 3 == 0 { continue; } println!("{n}"); }}Output:
124578102. Find a pair that multiplies to 24
Section titled “2. Find a pair that multiplies to 24”Use two nested for loops, x and y, both over 1..=9. Find the first pair where x * y == 24, print it, and stop both loops. Hint: put a label on the outer loop and use break 'outer;.
Show solution
fn main() { 'outer: for x in 1..=9 { for y in 1..=9 { if x * y == 24 { println!("Found it: {x} x {y} = 24"); break 'outer; } } } println!("Done searching");}Output:
Found it: 3 x 8 = 24Done searchingTry changing break 'outer; to a plain break;. Now only the inner loop stops, so the search goes on and also prints 4 x 6, 6 x 4 and 8 x 3.
breakexits the loop immediately.continueskips the rest of the current iteration and moves to the next one.- A label (
'outer:) letsbreak/continuetarget an outer loop from inside a nested one. - In a
whileloop, update your counter before anycontinue, or the loop can get stuck forever.
Quick check
Section titled “Quick check”Quick check
1. What does continue do inside a loop?
2. How do you write a label on a loop so break can target it from a nested loop?
3. A while loop with continue never finishes. What is the most likely cause?
Score: 0 / 3
Read more
Section titled “Read more”- Loop Labels to Disambiguate Between Multiple Loops — The Rust Book: the official introduction to labels, with a nested countdown example.
- Nesting and labels — Rust by Example: a short runnable example of
break 'outerandcontinue 'outer. - Loop Labels — The Rust Reference: the exact rules for where labels can be used.
breakexpressions — The Rust Reference: howbreakworks, includingbreakwith a value.continueexpressions — The Rust Reference: whatcontinuedoes in each kind of loop.