liveBook · Manning

IntoIterator

As explained in Idiomatic Rust

IntoIterator is a Rust trait that defines how a value is converted into an iterator, including iteration by ownership or reference.

`IntoIterator` defines how a value becomes an iterator. For a collection, implement it separately for the owned value, a shared reference, and a mutable reference when those forms should yield different items. Keep iteration state in dedicated iterator types rather than implementing `Iterator` on the collection itself. section 3.2.4

impl<'a, T> IntoIterator for &'a LinkedList<T> {
    type IntoIter = Iter<'a, T>;
    type Item = &'a T;
    fn into_iter(self) -> Self::IntoIter {
        self.iter()       #1
    }
}
impl<'a, T> IntoIterator for &'a mut LinkedList<T> {
    type IntoIter = IterMut<'a, T>;
    type Item = &'a mut T;
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()      #2
    }
}
impl<T> IntoIterator for LinkedList<T> {     #3
    type IntoIter = IntoIter<T>;
    type Item = T;
    fn into_iter(self) -> Self::IntoIter {
        self.into_iter()        #4
    }
}

The three `LinkedList` implementations delegate to `iter()`, `iter_mut()`, and `into_iter()`, yielding `&T`, `&mut T`, and `T` respectively. section 3.2.4

Iterate without consuming the list

`for data in &linked_list { ... }` uses the shared-reference implementation, so each `data` is a borrowed item. Use the owned form when iteration should consume the list. section 3.2.4

The return type matters: an initial linked-list implementation returned `Rc<RefCell<T>>` from all three iterator types, obscuring the intended ownership differences. Its consuming iterator instead tries to unwrap those shared pointers to yield `T`; that can fail when other owners remain. section 3.2.4

How Idiomatic Rust covers it

How deep it goes
Works through three trait implementations and their item types, including the consuming iterator’s pointer-unwrapping constraint.
Examples
Uses a linked list of dinosaur names and a borrowed `for` loop.
What it assumes
Assumes familiarity with Rust traits, generics, and ownership while developing the collection-specific iterator implementations.
What you can do after
Build it from scratch for owned and borrowed linked-list iteration using dedicated iterator types.
How it connects
Connects `Iterator`, `for` loops, ownership, borrowing, and `Rc<RefCell<T>>`.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Idiomatic Rust. The section links point to the parts of the book it draws on.