3 ms·
Arithmetic still happens a lot in programs, and especially so in tight loops (most tight loops will be filled with arithmetic, rather than anything else), even
by dbaupp 10y ago
Arithmetic still happens a lot in programs, and especially so in tight loops (most tight loops will be filled with arithmetic, rather than anything else), even if it doesn't happen as the loop counters.
You are correct that Rust does bounds checks in release mode, but fortunately there's no extra bounds checks needed:
- all indexing is done with unsigned values, and,
- even if signed values were used, the '< 0' comparison is still not necessary because one can first cast to unsigned (a noop) and then do a single '< length' comparison: negative values will be huge, at least as large as MEMORY_SPACE/2, which is in fact the limit on array lengths in Rust[1].
[1]: http://doc.rust-lang.org/stable/nomicon/vec-alloc.html http://doc.rust-lang.org/stable/nomicon/vec-alloc.html search for isize::MAX.