News for hackers. Stuff that matters.
Re:Rust, Lifetimes, and Collections by veddan on Wednesday November 19, 2014 @01:47PM (#8631794)
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.

There are no comments on this comment yet.