The IntoIterator trait converts a type into an iterator, enabling it to be used in for loops and iterator methods. Implementing this trait allows your custom types to be consumed item-by-item just like standard collections.
use std::iter::IntoIterator;
struct MyCollection {
items: Vec<i32>,
}
impl IntoIterator for MyCollection {
type Item = i32;
type IntoIter = std::vec::IntoIter<i32>;
fn into_iter(self) -> Self::IntoIter {
self.items.into_iter()
}
}