Excellent idea.
But it seems hard for rust to achieve it.
IIRC, for logically call the next function for Iterators, and Rust have less knowledge with iterators.
i.e., if you wrote:
let mut a:i32;
let mut b:i32=0i32;
for i in 0..10 {
b+=i*i;
a=b;
}
dbg!(a);
the program won't compile since Rust could not inference whether a is initialized:
4 | for i in 0..10 {
| ---- if the `for` loop runs 0 times, `a` is not initialized
It might be hard to do such optimize.