How to make a reverse for loop in Rust? (reverse iteration order)
Hello everyone! I recently started learning Rust and encountered a problem when writing a reverse loop. In Python, I'm used to doing `for i in range(10, 0, -1)` to iterate numbers from 10 to 1. In Rust, I tried to write something similar:
```rust
for i in 10..0 {
println!("{}", i);
}
```
But this code simply outputs nothing. I expected it to output 10, 9, 8... 1, but the loop doesn't execute even once. Apparently, the range `10..0` is empty because Rust by default only goes in ascending order.
I also tried using `for i in (0..10).rev()` — this works, but outputs numbers from 9 to 0, and I need from 10 to 1 (inclusive). How do I correctly make a reverse loop including both the start and end values? And is there a way to do this without `.rev()`, for example, using a regular
No answers yet.
Log in to leave an answer