Rust Loop Control: Mastering Break and Continue Statements Learn how to control the flow of your Rust loops with break and continue statements. Discover how to terminate loops prematurely, skip iterations, and combine these statements to write more efficient code.

Mastering Loop Control in Rust: Break and Continue Statements

When working with loops in Rust, you often need to alter their flow to achieve your desired outcome. This is where the break and continue statements come in – powerful tools that help you control the execution of your loops.

Terminating Loops with Break

The break statement is used to terminate the execution of a loop prematurely. It’s like hitting the emergency stop button – as soon as the break statement is encountered, the loop stops running, regardless of the loop condition.

Let’s see an example:
rust
let mut number = 1;
while number <= 10 {
println!("{}", number);
number += 1;
if number == 6 {
break; // Exit the loop when number reaches 6
}
}

In this example, the loop will only print the first five natural numbers and then terminate.

Skipping Iterations with Continue

The continue statement, on the other hand, allows you to skip the current iteration of a loop and move on to the next one. It’s like fast-forwarding to the next iteration – the current iteration is skipped, and the loop continues with the next one.

Here’s an example:
rust
let mut number = 1;
while number <= 10 {
if number == 3 {
number += 1;
continue; // Skip the current iteration when number is 3
}
println!("{}", number);
number += 1;
}

In this example, the loop will skip printing the number 3 and continue with the next iteration.

Break and Continue in Nested Loops

But what happens when you use break and continue in nested loops? Let’s find out!

Break in Nested Loops

When you use break in a nested loop, it will only terminate the innermost loop. The outer loop will continue to run normally.

Here’s an example:
rust
let mut i = 0;
while i < 3 {
let mut j = 0;
while j < 5 {
if j == 3 {
break; // Exit the inner loop when j reaches 3
}
print!("*");
j += 1;
}
println!();
i += 1;
}

In this example, the inner loop will terminate when j reaches 3, but the outer loop will continue to run, printing three lines of asterisks.

Continue in Nested Loops

Similarly, when you use continue in a nested loop, it will only skip the current iteration of the innermost loop. The outer loop will continue to run normally.

Here’s an example:
rust
let mut i = 0;
while i < 3 {
let mut j = 0;
while j < 5 {
if j == 3 {
j += 1;
continue; // Skip the current iteration when j reaches 3
}
print!("*");
j += 1;
}
println!();
i += 1;
}

In this example, the inner loop will skip the iteration when j reaches 3, but the outer loop will continue to run, printing three lines of asterisks.

Combining Break and Continue

You can also use break and continue together to control the flow of your program. For example:
rust
let mut number = 1;
while number <= 10 {
if number == 3 {
number += 1;
continue; // Skip the current iteration when number is 3
}
if number > 5 {
break; // Exit the loop when number exceeds 5
}
println!("{}", number);
number += 1;
}

In this example, the loop will skip printing the number 3 and terminate when the number exceeds 5.

With these powerful tools, you can now master the art of controlling loops in Rust and write more efficient and effective code.

Leave a Reply

Your email address will not be published. Required fields are marked *