In this tutorial, we will learn about the continue statement in Rust. We will cover the basics of using the continue statement to skip the current iteration of a loop and proceed with the next iteration.
A continue statement is used to skip the current iteration of a loop and proceed with the next iteration. When a continue statement is encountered, the remaining code inside the loop for the current iteration is skipped, and the loop continues with the next iteration.
The syntax for the continue statement in Rust is:
continue;
The continue statement can be used in for loops, while loops, and loop blocks to skip the current iteration and proceed with the next iteration.
fn main() {
for i in 1..=10 {
if i % 2 == 0 {
continue;
}
println!("{}", i);
}
}
1 3 5 7 9
i
and initialize it to 1.i
is less than or equal to 10.i
is odd.fn main() {
let mut i = 1;
while i <= 10 {
if i % 2 != 0 {
i += 1;
continue;
}
println!("{}", i);
i += 1;
}
}
2 4 6 8 10