liveBook · Manning

Into_iter

As explained in Learn Rust in a Month of Lunches

into_iter is a Rust method that converts a collection or other iterable value into an iterator, with item ownership determined by the receiver.

An iterator gives you items one at a time, like dealing cards from a deck. Call `.into_iter()` on a vector when you want an iterator that takes its items by value rather than borrowing them. The vector is consumed, so you cannot use it afterward. section 8.2

Choose the method that matches what you need to do with the items:

  • `.iter()` borrows items as references, leaving the vector available. section 8.2
  • `.iter_mut()` borrows items as mutable references so you can change them. section 8.2
  • `.into_iter()` takes items by value, consuming the vector. section 8.2
fn main() {
    let vector1 = vec![1, 2, 3];
   let vector1_a = vector1
        .iter()                   #1
        .map(|x| x + 1)
        .collect::<Vec<i32>>();
    let vector1_b = vector1
        .into_iter()
        .map(|x| x * 10)
        .collect::<Vec<i32>>();
    let mut vector2 = vec![10, 20, 30];
    vector2.iter_mut().for_each(|x| *x +=100);
 
    println!("{:?}", vector1_a);
    println!("{:?}", vector1_b);
    println!("{:?}", vector2);
}

The first chain uses `.iter()` to make a new vector while keeping `vector1`. The second uses `.into_iter()` to make another vector, consuming `vector1`. In both chains, `.collect()` turns the iterator's results into a `Vec`. section 8.2

You can also pass an owned vector directly to a `for` loop instead of writing `.into_iter()` explicitly. Once you have an iterator, methods such as `.map()` can transform its items; an iterator chain does no work until it is consumed, for example by `.collect()`. section 8.2 section 8.3.2

use std::collections::HashMap;
 
fn main() {
    let some_keys = vec![0, 1, 2, 3, 4, 5];
    let some_values = vec!["zero", "one", "two", "three", "four", "five"];
 
    let number_word_hashmap = some_keys
        .into_iter()                     #1
        .zip(some_values.into_iter())    #2
        .collect::<HashMap<_, _>>();
 
    println!(
        "The value at key 2 is: {}",
        number_word_hashmap.get(&2).unwrap()
    );
}

Two vectors become value iterators. `.zip()` pairs their items, and `.collect()` builds a `HashMap` from those pairs. section 8.3.2

How Learn Rust in a Month of Lunches covers it

How deep it goes
Works through ownership and vector transformations, but does not explain how to implement `into_iter`.
Examples
Shows vectors transformed into new vectors and two value iterators zipped into a `HashMap`.
What it assumes
Builds on vectors and `for` loops while briefly defining iterators and deferring a fuller explanation of closures.
What you can do after
Apply it to choose value iteration and recognize when a vector will no longer be available.
How it connects
Connects owned iteration to borrowing, mutable references, `for` loops, `map`, `zip`, and `collect`.

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 Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.