This is also more similar to how rustc desugars them.
That's actually not magic but Rust' syntactic sugar in action. for loops accept iterators. Yet, some instances can be transformed into iterators "on the fly". The type must implement the IntoIterator trait and its into_iter() function to be eligible. As seen from the diagram above, that's the case of Vec.
Actually, it's the other way around. for loops accept IntoIterators. However, every Iterator also impls IntoIterator, by this blanket impl.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nice article!
Two comments:
A much better way to desugar a
for
loop is the following:This is also more similar to how rustc desugars them.
Actually, it's the other way around.
for
loops acceptIntoIterator
s. However, everyIterator
also implsIntoIterator
, by this blanket impl.