Modern languages have for value in arr, arr.forEach or equivalent. Such code makes it easier for compiler writers to make this optimization.
Swift even chose to remove for loops that step a variable (https://github.com/apple/swift-evolution/blob/main/proposals...), and I don’t think Rust has them, either.
I think that’s the right decision. In modern languages, classical for loops are a code smell.
And of course, golang goes the other way. https://go.dev/tour/flowcontrol/1:
Go has only one looping construct, the for loop.
The basic for loop has three components separated by semicolons:
- the init statement: executed before the first iteration
- the condition expression: evaluated before every iteration
- the post statement: executed at the end of every iteration
Those features were modern 40 years ago, even Visual Basic (pre-.NET) already had them.
for (x in array)
print(x)
->
const __local0017 = array.len;
for (var __local0018 = 0; __local0018 < __local0017; __local++) {
const x = array[__local0018];
print(x);
}
The middle and back ends will infer that __local0018 doesn't exceed __local0017 which is the length of array and get rid of the bound check.