I would expect the optimizer to elide all bounds checks in this particular example. This a trivial case: The loop condition is it < len
. The bounds check condition is it < len
, too, and so it can be optimized out. You can verify this by inspecting the optimized (release mode) code.
Still, in this situation, prefer iterators. Using for element in some_slice {
is very easy. Use for (index, element) in some_slice.iter().enumerate() {
if you need the index too.