In most cases, you use iterators rather than indexing
for x in v.iter_mut() {
// do some work with x
}
or if you need the index for (i, x) in v.iter_mut().enumerate() {
// do some work with x and i
}
similar to Python's enumerate(). These loops never need to do any bounds-checking, even when compiling without optimization.In this example the iteration count is a small compile-time constant (5), and the loop body is small, so when compiling with optimizations the loop will be unrolled.
> I strongly suspect that there is a way in Rust to iterate over a vector with only a single hoisted bounds-check. Hopefully other Rust people can chime in on what this is.
Iterators.
Indent by four spaces to get a <pre> tag:
> This seems to be a forgotten technology.Given what I know about Rust, I strongly suspect that there is a way in Rust to iterate over a vector with only a single hoisted bounds-check. Hopefully other Rust people can chime in on what this is.