Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The worse bug I fought in one of my rust programs was when using the range type. I needed to go from n to 1 included, so, from n included to 0 excluded. Of course `(n..0)` didn't work, so I tried `(0..n).rev()`, which didn't work either (first, you generate all numbers in [0;n-1], then you reverse the list), and I can't blame the language, it makes sense, but it's highly unintuitive (in python for instance, `range(n, 0, -1)` does what is expected).

I'm not a seasoned rust programmer though, is there a better, more explicit way to do what I attempted there?



I think (1..=n).rev() will do what you want?

A .step_by() that accepts negative values a la python would be nice, but I can live without.

I'd love a REPL for this sort of thing though - a website just isn't as good.


Thank you! I didn't know about the `=n` syntax.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: