liveBook · Manning

Into_iter

As explained in Idiomatic Rust

into_iter is a method that consumes a collection or other iterable value and returns an iterator that yields its owned elements.

Use `into_iter()` when an iterator should take ownership of its input and yield owned items. For a collection, this is the consuming counterpart to `iter()`, which yields shared references, and `iter_mut()`, which yields mutable references. Choose it when later operations need to move values out rather than borrow them. section 3.2.4 section 3.3

A custom collection should usually return a separate stateful iterator instead of implementing `Iterator` directly on the collection. An inherent `into_iter(self)` method consumes the collection and returns that iterator. Implementing `IntoIterator` also lets owned, shared-borrowed, and mutably borrowed forms select their corresponding iterators. 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 `IntoIterator` implementations distinguish consuming the linked list from iterating over `&LinkedList` or `&mut LinkedList`. section 3.2.4

Move results out of a vector

After partitioning parsed values, consume each vector to unwrap its results: ```rust let successes: Vec<_> = successes.into_iter().map(Result::unwrap).collect(); let failures: Vec<_> = failures.into_iter().map(Result::unwrap_err).collect(); ``` The resulting vectors contain integers and parse errors, respectively, rather than `Result` wrappers. section 3.2.5

Consuming the collection is not, by itself, a guarantee that a custom iterator can extract every value. In the linked-list implementation, `next()` uses `Rc::try_unwrap` to obtain owned items and returns `None` if unwrapping fails. Take care when shared ownership remains in an iterator design. section 3.2.4

`Result::into_iter()` is another useful case: it yields the `Ok` value when present and nothing for `Err`. That makes `flat_map()` convenient for keeping successful parses, but it silently discards failures. Use `partition()` when both successes and errors matter. section 3.2.5

How Idiomatic Rust covers it

How deep it goes
Works through separate iterator structs, `IntoIterator` implementations, and owned-item extraction with `Rc::try_unwrap`.
Examples
Shows a consuming linked-list traversal and vectors consumed to unwrap partitioned parsing results.
What it assumes
Assumes working familiarity with Rust traits and ownership while developing the linked-list iterator implementations step by step.
What you can do after
Build it from scratch for a linked list, or apply it to move vector elements through iterator chains.
How it connects
Connects `Iterator`, `IntoIterator`, ownership, borrowing, `flat_map`, `Result`, and `partition`.

Also explained in

Compare how 2 books explain into_iter →

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.