For loop structure


#1

for (i, x) in (0…20).zip((0…15).step_by(2))

I know how a C - like for loop works but how does this work? What is the zip and when the user types step_by(2), how does it know to increment the x variable by two and not the i value?


#2

Hi.
You can read about zip and step_by


#3

When you make iterators with 0..20, zip(), step_by() they all return structs with their settings, so in the end they combine to a struct that is something like:

struct zip {
   first: range {start: 0, end: 20},
   second: step {
     by: 2,
     of: range {start: 0, end: 15},
   }
}

and this whole struct implements Iterator (or IntoIterator) that knows how to use all this information to produce (i,x) pairs.