How it works...
Iterators are a great way of providing advanced capabilities to custom data structures. With their simple, unified interface, collection types can be switched out easily as well and programmers don't have to get used to new APIs for every data structure.
By implementing the Iterator trait in Steps 1 and 2, it becomes easy to provide exactly the desired access level to a collection's elements. In the case of this recipe (and similar to Vec<T>), it will consume the list entirely and remove the items one by one, starting at the front.
In Step 3, we implement IntoIterator, a trait that makes this construct available to the for loop and other users who call into_iter(). Not every collection implements this trait to provide multiple ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access