Skip to content

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 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:

1
2
3
4
▶ Try it Yourself

Even though the range goes up to 10, the loop stops the moment i hits 5.

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:

1
3
5
7
9
▶ Try it Yourself

Every even number gets skipped — continue fires, and the loop moves straight to the next value of i without running println!.

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 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
2 x 2 = 4
▶ Try it Yourself

Without 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.

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:

1
2
4
5
▶ Try it Yourself

Moving 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.

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:

1
2
4
5
7
8
10
▶ Try it Yourself

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 = 24
Done searching
▶ Try it Yourself

Try 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.

  • break exits the loop immediately.
  • continue skips the rest of the current iteration and moves to the next one.
  • A label ('outer:) lets break/continue target an outer loop from inside a nested one.
  • In a while loop, update your counter before any continue, or the loop can get stuck forever.

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