An iterator provides a way to visit values one at a time without making each caller manage traversal. In Rust, the Iterator trait makes that convention reusable: implement its small core, then use methods such as map(), filter(), fold(), and last(). Iterators also pair naturally with closures to express operations on data structures. section 3.2.3 section 3.3
trait Iterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
}
The essential implementation supplies an associated Item type and next(&mut self) -> Option<Self::Item>. Each call asks for the next item; None represents the end. section 3.2.3
Traversing a linked list
A linked list can store its current traversal position and implement next() to return the head first, then follow each node’s next pointer. Its Item is a pointer to a list node. With Iterator implemented, last() finds the final node; the example then uses that node to append another item. section 3.2.3
impl<T> Iterator for LinkedList<T> {
type Item = ListItemPtr<T>; #1
fn next(&mut self) -> Option<Self::Item> {
match &self.cur_iter.clone() { #2
None => {
self.cur_iter = Some(self.head.clone()); #3
}
Some(ptr) => {
self.cur_iter = ptr.borrow().next.clone(); #4
}
}
self.cur_iter.clone() #5
}
}
The implementation shows the traversal state, the associated Item type, and next() advancing through linked nodes. section 3.2.3
Ownership matters when choosing an iterator. Typically, iter() yields references to items, while into_iter() moves items out of the underlying sequence. For a library interface, accepting an iterator can also allow more kinds of input than requiring a Vec; a slice is another, somewhat less flexible option. Choose the interface your callers need rather than introducing a custom collection by default. section 3.3 section 6.4














