Search code examples
rustiterator

Numerically decreasing iterator


I need an open-ended decreasing integer iterator in Rust. (..=n).rev() does not work. The closest I've found is std::iter::successors(Some(n), |n| Some(n - 1)), which seems needlessly convoluted.

Is there a clearer way to express this notion of a numerically decreasing iterator?


Solution

  • It's not exactly open-ended, but you could use the minimum value for whatever integer type you're targeting:

    (i64::MIN..=n).rev()