4 ms·
Rust's solution to this is quite good, that's 0..9 and if you want to include 9 it's 0..=9, it looks a bit funny but knowing one with an = sign in it exists rem
by reorder9695 1y ago
Rust's solution to this is quite good, that's 0..9 and if you want to include 9 it's 0..=9, it looks a bit funny but knowing one with an = sign in it exists removes any doubt
- mirsadm 1y agoAdding additional syntax to a language for this case seems bonkers to me. People can just write 0..10.
- deleted 1y ago[deleted]
- dgrunwald 1y agoIf you need `0..=n`, you can't write `0..(n+1)` because that addition might overflow.
- hgomersall 1y agoI'm actually curious now how this is stored on `Range` in rust. I've certainly used ..= for exactly the reason you say, but as far as I'm aware `.end` on the range is the exclusive upper bound in all cases. What happens to `.end` in the overflowing case? Edit: it doesn't use Range for ..=, but rather RangeInclusive, which works fine.
- zygentoma 1y agoIt's more meant for usage with variables: for i in 0..length { … } for i in 0..=maxindex { … }