Start learning free

LESSON 129 OF 414 · WHILE LOOPS & FOR LOOPS

Printing Numbers in Rust with a For Loop and Ranges

Practice Read the full explanation

We can achieve many different outcomes with for loops.

It's all about tuning the start, end and step of the range.

For example, we can print all numbers from 2 through 6:

for i in 2..7 {
	println!("{}", i);
}

The output of this code is:

2
3
4
5
6

Let's break down this code:

  • The range starts at 2, so we start iterating from number 2.

  • The range end is 7 (exclusive), so we stop at 6. We could've written 2..=6 and it would still be correct.

  • We advance by 1 each iteration automatically.


Assignment

Let's print all numbers from 3 through 10 using a for loop.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    RUST · WHILE LOOPS & FOR LOOPS

    Printing Numbers 3 in Rust, explained

    Problem overview

    The task is to print every whole number from 3 through 10, one per line, using a for loop. The expected output is 3, 4, 5, 6, 7, 8, 9 and 10.

    In Rust the numbers a for loop visits come from a range, so the whole exercise is about writing the right range. The start, the end and whether the end is included are all decided by the range expression.

    Approach: choose the start and the end of the range

    Rust has two kinds of range, and the difference is one character:

    • a..b starts at a and stops before b. The end is excluded.
    • a..=b starts at a and ends at b. The end is included.

    For example, the range 2..7 gives 2, 3, 4, 5, 6, and printing it with a loop shows the numbers 2 through 6. The inclusive form 2..=6 gives exactly the same numbers.

    for i in 2..7 {
        println!("{}", i);
    }

    It prints 2, 3, 4, 5 and 6, each on its own line. The step is always 1 for a plain range: the loop variable takes each next number automatically, so there is no update to write.

    For this lesson the last number to print is 10. There are two correct ways to say that. One is 3..11, where the end is the first number not printed. The other is 3..=10, where the end is the last number printed. The second reads like the sentence in the task, "from 3 through 10", so it is easier to get right.

    Step by step

    1. Write the loop header for i in 3..=10 with the braces. The variable i takes the values 3, 4, ..., 10 in turn. The = is what makes 10 part of the range.
    2. Print the variable inside the body with println!("{}", i);. The {} placeholder is replaced by the value of i, and println! adds a newline after it, so each number appears on its own line.

    The solution in Rust

    for i in 3..=10 {
        println!("{}", i);
    }

    The details a learner might trip on:

    • The type of i. Nothing says what kind of integer the range holds, so Rust uses its default, i32. This is enough for small numbers, and it handles negative values too.
    • The loop variable belongs to the loop. You do not write let or mut for it. It is created on each pass and gone after the closing brace.
    • The end is not special in ... Writing 3..10 gives 3 through 9, because 10 is excluded. This is the most common off-by-one error with ranges.
    • Counting the passes. For a..b the body runs b - a times, and for a..=b it runs b - a + 1 times. Here that is 10 - 3 + 1 = 8.

    Edge cases and common mistakes

    • Dropping the last number. 3..10 stops at 9. If the task says "through 10", use 3..=10 or 3..11.
    • Adding one too many. 3..=11 prints 11 as well.
    • An empty range. If the start is above the end, as in 10..=3, the range is empty and the loop body never runs. Nothing is printed and nothing is reported. A Rust range only counts upward; counting down is covered in another lesson.
    • A range of one number. 3..=3 runs once, and 3..3 never runs.
    • Forgetting the ! in println!. It is a macro, and the compiler says so.
    • Using ++ or an update statement. A for loop over a range has no update.

    Testing

    Try these ranges in the editor above and compare with the numbers you expect:

    • 3..=10 prints 3 to 10, which is eight lines.
    • 3..10 prints 3 to 9, which is seven lines.
    • 3..=3 prints a single line with 3.
    • 10..=3 prints nothing.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free