How to write a loop in Rust with a decreasing range including the 0?

Viewed 6279

I need to write a loop in Rust that runs its body from a value down to and including n = 0. At first, I wrote it like this:

for n in max..0 {
    // ...
}

However, it just runs until n = 1.

The best alternative I can think of is this:

let mut n = max;
loop {
    // ...

    if n == 0 {
        break;
    }
    n -= 1;
}

This solution is really clumsy and doesn't satisfy me. Is there a better way of writing that loop, e.g. a for-loop with a range including the right boundary value of the ..?

0 Answers
Related