pub trait Iterator {type Item;fn next(&mut self) -> Option<Self::Item>;...}
Iterator
has an associated type Item
(and not a parameterized type) because we only have one implementation of Item for. The associated type also means the typechecker only needs to choose from one possible implementation. A parameterized type would require the typechecker to look up amongst potentially multiple implementations.
trait std::iter::IntoIterator
A trait that represents a thing that can be turned into an iterator
This allows you to write functions that operate on arguments that can be turned into an iterator without requiring the user to pass the iterator in.
If using a for loop you don't need to explicitly call .iter(), however there are behavioral differences. Given a vector xs:
let xs = vec![2,4,6]
This example gives you an owned "x" value.
for x in xs {}
While both of these (which are equivalent because into_iter
is defined for references to slices) will borrow the x values.
for x in xs.iter() {}for x in &xs {}