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..bstarts ataand stops beforeb. The end is excluded.a..=bstarts ataand ends atb. 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
- Write the loop header
for i in 3..=10with the braces. The variableitakes the values 3, 4, ..., 10 in turn. The=is what makes 10 part of the range. - Print the variable inside the body with
println!("{}", i);. The{}placeholder is replaced by the value ofi, andprintln!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
letormutfor it. It is created on each pass and gone after the closing brace. - The end is not special in
... Writing3..10gives 3 through 9, because 10 is excluded. This is the most common off-by-one error with ranges. - Counting the passes. For
a..bthe body runsb - atimes, and fora..=bit runsb - a + 1times. Here that is 10 - 3 + 1 = 8.
Edge cases and common mistakes
- Dropping the last number.
3..10stops at 9. If the task says "through 10", use3..=10or3..11. - Adding one too many.
3..=11prints 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..=3runs once, and3..3never runs. - Forgetting the
!inprintln!. It is a macro, and the compiler says so. - Using
++or an update statement. Aforloop over a range has no update.
Testing
Try these ranges in the editor above and compare with the numbers you expect:
3..=10prints 3 to 10, which is eight lines.3..10prints 3 to 9, which is seven lines.3..=3prints a single line with 3.10..=3prints nothing.
