3 ms·
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
by veddan 12y ago
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.