> for i in 0..(xs.len()) {
let x = xs[i];
// do something with x
}
> should really be this:
> for x in &xs { // do something with x }
I am curious why the compiler can't rewrite the former to the latter?
> for x in &xs { // do something with x }
I am curious why the compiler can't rewrite the former to the latter?
Though this might be information that rustc knows about but not LLVM.
The second just increments a pointer which starts at the first elem of the array; the first increments a counter and then uses it as an offset into the array, which is bounds checked (the second never generates bounds checks). Proving that that counter never exceeds the end of the array, so the bounds check can be dropped, is not trivial.
(I may have gotten something wrong, this is my first Rust program! Yay!)