Rust has three types of loops: loop (infinite), while (runs while a condition is true), and for (used to iterate over ranges or collections). You can control loops using break to exit early or continue to skip to the next iteration.
Most programs need to repeat actions. In Rust, you have three ways to do this: loop, while, and for. Each one is suited for a different kind of task. Rust’s looping style is quite different from what we are used to in JavaScript/Node.js or Python. In this post, I will explain each type, how to use them, and how to control loop behavior using break and continue.
If you are not familiar with conditions yet, please read my post on Decision Making in Rust before continuing.
The loop Keyword
The simplest kind of loop in Rust is loop. It repeats forever until you tell it to stop using break.
Example:
fn main() {
let mut count = 0;
loop {
println!("Count is: {}", count);
count += 1;
if count == 5 {
break;
}
}
}
This prints numbers from 0 to 4. The loop ends when count == 5.
Use loop when you want to repeat something an unknown number of times and stop only under certain conditions.
Using while Loops
A while loop runs as long as a condition is true.
fn main() {
let mut number = 3;
while number != 0 {
println!("Number: {}", number);
number -= 1;
}
println!("Done!");
}
This is useful when the end condition depends on the data, not a fixed range.
Using for Loops
A for loop is used to loop through a range or collection. It is the most common type of loop in Rust.
fn main() {
for number in 1..4 {
println!("Number is: {}", number);
}
}
This prints:
Number is: 1
Number is: 2
Number is: 3
The range 1..4 includes 1, 2, and 3. It stops before 4. If you want to include the last number, use 1..=4.
You can also loop over arrays or vectors:
let names = ["Alice", "Bob", "Charlie"];
for name in names {
println!("Hello, {}", name);
}
Controlling Loops with break and continue
You can exit a loop early using break, and skip the current iteration using continue.
for number in 1..=5 {
if number == 3 {
continue; // skip 3
}
if number == 5 {
break; // stop the loop
}
println!("Number: {}", number);
}
This will print:
Number: 1
Number: 2
Number: 4
Labeled Loops
If you have nested loops, you can label them and choose which one to break:
'outer: for i in 1..=3 {
for j in 1..=3 {
if i == 2 && j == 2 {
break 'outer;
}
println!("i = {}, j = {}", i, j);
}
}
Here ‘outer is a label attached to the outer for loop.
This stops both loops when i == 2 and j == 2.
Choosing the Right Loop
Use This | When You Need To |
---|---|
loop | Repeat until manually stopped |
while | Check a condition each time |
for | Loop over a range or collection |
Summary
Rust gives you three ways to write loops: loop (manual control), while (condition-based), and for (range or data-based). You can use break and continue to fine-tune the flow. Labeled loops are helpful in nested situations. Once you get used to them, loops in Rust feel simple and safe.
Next, I will walk you through Functions in Rust. You will learn how to group logic, pass values, and return results cleanly.