Question
I want to print FizzBuzz in reverse order in Rust, counting from 100 down to 1.
I tried this code:
fn main() {
for n in std::iter::range_step(100u, 0, -1) {
if n % 15 == 0 {
println!("fizzbuzz");
} else if n % 3 == 0 {
println!("fizz");
} else if n % 5 == 0 {
println!("buzz");
} else {
println!("{}", n);
}
}
}
The code compiles, but it does not print anything. How can I correctly iterate from 100 down to 1 in Rust?
Short Answer
By the end of this page, you will understand how reverse iteration works in Rust, why some descending loop attempts produce no output, and how to correctly loop from a larger number down to a smaller one using ranges and .rev(). You will also see practical examples, common mistakes, and a small reverse-counting mini-project.
Concept
In Rust, a for loop works by iterating over an iterator. That means the important question is not just “what numbers do I want,” but also “what iterator produces them?”
For counting upward, Rust ranges are straightforward:
for n in 1..=5 {
println!("{}", n);
}
This prints 1 through 5.
For counting downward, Rust does not usually use a negative step directly in a range expression. Instead, the common Rust style is:
- Create a normal forward range.
- Reverse it with
.rev().
Example:
for n in (1..=5).rev() {
println!("{}", n);
}
This prints 5 down to 1.
This matters because Rust's iterator system is designed to be explicit, safe, and composable. Rather than special loop syntax for every situation, Rust encourages you to build the iterator you want. Reversing an iterator is one of the most common tools for this.
Mental Model
Think of a Rust range like a list of pages in a book.
1..=5means: pages 1, 2, 3, 4, 5.rev()means: read those pages from the back to the front
So instead of asking Rust to “walk backward with a negative step,” you usually tell it:
- “Make the normal sequence first”
- “Now reverse it”
That is much like stacking numbered cards from 1 to 100, then flipping the stack over and reading from the top.
Syntax and Examples
The most common way to write a reverse loop in Rust is:
for n in (start..=end).rev() {
// use n
}
Example: count from 5 down to 1
fn main() {
for n in (1..=5).rev() {
println!("{}", n);
}
}
Output:
5
4
3
2
1
Example: reverse FizzBuzz from 100 to 1
fn main() {
for n in (1..=100).rev() {
if n % 15 == 0 {
println!("fizzbuzz");
} else if n % 3 == {
();
} n % == {
();
} {
(, n);
}
}
}
Step by Step Execution
Consider this code:
fn main() {
for n in (1..=5).rev() {
println!("{}", n);
}
}
Here is what happens step by step:
-
Rust creates the range
1..=5.- This represents the values
1, 2, 3, 4, 5.
- This represents the values
-
.rev()is called on that range.- The iterator is reversed.
- Now it will yield
5, 4, 3, 2, 1.
-
The
forloop starts.- First iteration:
n = 5 println!prints5
- First iteration:
-
Second iteration:
n = 4println!prints
Real World Use Cases
Reverse iteration appears in many real programs, not just counting examples.
Common practical uses
-
Displaying recent items first
- Show newest notifications before older ones.
-
Countdowns
- Timers, launch countdowns, game start sequences.
-
Processing collections from the end
- Some algorithms inspect the last items first.
-
Undo history
- Traverse recent actions in reverse order.
-
Log analysis
- Read recent events before older records.
Example: countdown
fn main() {
for seconds in (1..=10).rev() {
println!("{}...", seconds);
}
println!("Go!");
}
Example: iterating a vector in reverse
fn main() {
= [, , ];
messages.().() {
(, message);
}
}
Real Codebase Usage
In real Rust codebases, developers often use reverse iteration with iterator adapters rather than manual index manipulation.
Common patterns
Reverse a range
for i in (0..10).rev() {
println!("{}", i);
}
Useful for countdowns or reverse traversal.
Reverse a collection iterator
let items = vec![1, 2, 3, 4];
for item in items.iter().rev() {
println!("{}", item);
}
This avoids indexing and is safer.
Combine with filters or mapping
let nums = vec![1, 2, 3, 4, 5, ];
nums.().().(|n| **n % == ) {
(, n);
}
Common Mistakes
1. Forgetting that .. excludes the end value
Broken example:
for n in (1..100).rev() {
println!("{}", n);
}
This prints 99 down to 1, not 100 down to 1.
Correct version:
for n in (1..=100).rev() {
println!("{}", n);
}
2. Expecting a negative step in modern range syntax
Beginners sometimes expect something like this to work:
// Not valid modern Rust range syntax for descending loops
for n in 100..1 {
println!(, n);
}
Comparisons
| Approach | Example | Best for | Notes |
|---|---|---|---|
for with reversed range | for n in (1..=100).rev() | Simple reverse counting | Most idiomatic for numeric countdowns |
while loop | while n >= 1 | Custom loop control | More manual, easier to make mistakes |
| Reverse collection iterator | items.iter().rev() | Traversing arrays, vectors, slices | Preferred over manual indexing |
| Forward range | for n in 1..=100 | Normal ascending iteration | Not for descending order unless combined with |
Cheat Sheet
// Count up
for n in 1..=5 {
println!("{}", n);
}
// Count down
for n in (1..=5).rev() {
println!("{}", n);
}
// Exclusive upper bound
for n in (1..5).rev() {
println!("{}", n); // prints 4, 3, 2, 1
}
// Manual countdown
let mut n = 5;
while n >= 1 {
println!("{}", n);
n -= 1;
}
Quick rules
- Use
..for an exclusive end. - Use
..=for an inclusive end. - Use
.rev()to reverse a range or iterator.
FAQ
How do I loop backwards in Rust?
Use a normal range and reverse it with .rev():
for n in (1..=10).rev() {
println!("{}", n);
}
Why does 100..1 not work the way I expect?
Rust ranges normally move forward. If the start is greater than the end, the range produces no values.
What is the difference between .. and ..= in Rust?
..excludes the end value..=includes the end value
So 1..100 stops at 99, while 1..=100 includes 100.
Is .rev() only for ranges?
No. It also works on many iterators, such as vectors, slices, and other iterable collections.
Should I use or for reverse loops?
Mini Project
Description
Build a small Rust program that prints a reverse countdown with custom messages. This project helps you practice descending for loops, inclusive ranges, and conditional logic together. It is similar to reverse FizzBuzz but simpler to modify and test.
Goal
Create a Rust program that counts from 10 down to 1 and prints special labels for certain numbers.
Requirements
- Count from 10 down to 1 using a
forloop. - Use an inclusive range and reverse it.
- Print
evenfor even numbers. - Print
multiple of 3for numbers divisible by 3. - Print the number itself for all other values.
Keep learning
Related questions
Accessing Cargo Package Metadata in Rust
Learn how to read Cargo package metadata like version, name, and authors in Rust using compile-time environment macros.
Associated Types vs Generic Type Parameters in Rust: When to Use Each
Learn when to use associated types vs generic parameters in Rust traits, with clear rules, examples, and practical API design advice.
Can a Struct Extend Another Struct in Rust? Composition vs Inheritance
Learn how Rust handles struct reuse without inheritance, using composition, traits, and wrapper structs with practical examples.