What continue does
The continue statement ends the current iteration of a loop and jumps to the next one. Any code below it in the loop body is skipped for that item. Where break leaves the loop completely, continue only skips one round. It is the natural way to say "ignore the items that don't qualify".
An example
This loop lets people into a bar only if they are at least 21:
let ages = vec![10, 30, 21, 19, 25];
for age in ages {
if age < 21 {
continue;
}
println!("Someone of age {} entered the bar.", age);
}
The output:
Someone of age 30 entered the bar.
Someone of age 21 entered the bar.
Someone of age 25 entered the bar.
For 10 and 19 the condition is true, so continue runs and the println! below it is skipped. For 30, 21 and 25 the condition is false, the if does nothing and execution falls through to the print. An age of exactly 21 is let in, because the test is < 21, not <= 21.
Details that matter in Rust
- References need a dereference. When you loop over
&heights, each item is an&i32. Writingheight < 170doesn't compile: the compiler reports a type mismatch, expecting a reference and finding an integer. Write*height < 170to compare the number the reference points to. - Printing needs no dereference.
println!("{}", height)prints a reference exactly as it prints the number it points to. - Borrowing keeps the vector.
for height in &heightsborrows the vector, so you can still useheightsafter the loop, for example to callheights.len(). Looping overheightsitself moves it into the loop. - Only the current iteration is skipped. After
continue, the loop moves on to the next item. In nested loops it affects only the innermost loop. - A trap in
whileloops. In awhileloop that increments its counter at the bottom, acontinueplaced before the increment jumps back to the condition without changing the counter, so the loop can repeat forever. Aforover a range or a vector advances on its own, so this doesn't happen. - An alternative shape. Instead of skipping the unwanted items, you can put the wanted case in the
if:if *height >= 170 { println!("{}", height); }. It behaves the same.continuereads better when the skipped case is a quick exception and the main work is long.
The exercise
The task is to let only people with a height of at least 170 into the arena, which here means printing their heights. The heights are 180, 139, 174, 190, 162.
- Declare the vector with
vec![180, 139, 174, 190, 162]and bind it toheights. - Loop over a reference to it. Write
for height in &heights. The vector is borrowed rather than consumed, and eachheightis an&i32. - Skip the short ones. Add
if *height < 170 { continue; }. The*reads the number so it can be compared with170. - Print the rest. Below the
if,println!("{}", height);runs only for heights of at least 170.
let heights = vec![180, 139, 174, 190, 162];
for height in &heights {
if *height < 170 {
continue;
}
println!("{}", height);
}
The output:
180
174
190
The heights 139 and 162 are skipped. The test < 170 means that a person exactly 170 tall would be let in, matching "at least 170".
Key takeaways
continueskips the rest of the current iteration and starts the next one.- Put the skip condition before the code that should be skipped.
- Dereference with
*when you compare the items of a borrowed vector with plain numbers. breakends the loop;continueonly skips one item.
