3 ms·
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 cour
by sacado2 6y ago
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?
- yodelshady 6y agoI 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.
- sacado2 6y agoThank you! I didn't know about the `=n` syntax.
- deleted 6y ago[deleted]