M Logo
📁 Rust for Beginners · Part 6 of 22

Rust Loops Explained: loop, while, for and Ranges

November 10, 2026

Welcome to episode 6 of Rust for Beginners, a series for complete beginners (no programming experience needed). In the last video, we learned about if and else. Today, we'll learn the three kinds of loops in Rust: loop, while, and for. Every code block below is the exact code from the video, ready to copy.

1. New project

Let's create a new project called loops, and open it in VS Code.

bash
cargo new loops
cd loops

2. loop

The simplest is loop. It repeats its body forever, until we stop it with break.

src/main.rs:

rust
fn main() {
    let mut count = 0;

    loop {
        count += 1;
        println!("Count: {count}");
        if count == 3 {
            break;
        }
    }

    let mut n = 1;
    let result = loop {
        n *= 2;
        if n > 100 {
            break n;
        }
    };
    println!("First power of two above 100: {result}");
}

Output: Count: 1 / Count: 2 / Count: 3 / First power of two above 100: 128

3. break

Here, we count up, and when count reaches three, break leaves the loop. Note that count is mutable, because it changes.

4. Return a value

loop can also return a value. We write it after break. Here, we keep doubling n, and break with the first value above one hundred.

5. Run A

Let's run it. One, two, three, and then one hundred twenty-eight.

bash
cargo run

6. while

A while loop runs as long as its condition is true. This one counts down from three.

rust
fn main() {
    let mut number = 3;

    while number > 0 {
        println!("{number}...");
        number -= 1;
    }
    println!("Liftoff!");
}

Output: 3... / 2... / 1... / Liftoff!

7. Run B

Three, two, one, liftoff!

bash
cargo run

8. for + range

The loop you'll use most is for. It goes through a range of numbers. One dot dot five means from one up to five, but not including five.

rust
fn main() {
    for i in 1..5 {
        println!("i = {i}");
    }

    let mut sum = 0;
    for i in 1..=100 {
        sum += i;
    }
    println!("Sum of 1 to 100: {sum}");

    for i in (1..=3).rev() {
        println!("{i}...");
    }
}

Output: i = 1 … i = 4 / Sum of 1 to 100: 5050 / 3... / 2... / 1...

9. Inclusive

To include the last number, add an equals sign. Here, we add up all the numbers from one to one hundred.

10. rev

And rev runs a range backwards.

11. Run C

Let's run it. i goes from one to four, the sum is five thousand and fifty, and then we count down.

bash
cargo run

12. Arrays

for also goes through the items of an array, one by one. No index is needed, so we can't go out of bounds.

rust
fn main() {
    let fruits = ["apple", "banana", "cherry"];
    for fruit in fruits {
        println!("I like {fruit}");
    }

    for i in 1..=10 {
        if i % 2 == 0 {
            continue;
        }
        print!("{i} ");
    }
    println!();
}

Output: I like apple / I like banana / I like cherry / 1 3 5 7 9

13. continue

continue skips the rest of this round and goes to the next one. Here, we skip the even numbers.

14. Run D

Let's run it. We like all three fruits, and we get only the odd numbers.

bash
cargo run

Recap

To recap: loop repeats until break, and can return a value. while runs while a condition is true. for goes through ranges and arrays. And continue skips to the next round.

© 2026 All Rights Reserved By Mahmudul Alam