std-method

into_iter — choose ownership through the receiver

For a Vec<T> passed by value, this transfers ownership of the vector and yields T. For &Vec<T> or &mut Vec<T>, the corresponding implementations yield references. Expected stdout (not executed): [3, 3] Common mistake: Using a moved Vec after creating its owned iterator.

Formal Syntax & Structure

rust syntaxrust
fn into_iter(self) -> Self::IntoIter

Working Example

Example Snippetrust
fn main() {    let names = vec![String::from("Ada"), String::from("Lin")];    let lengths: Vec<_> = names.into_iter().map(|name| name.len()).collect();    println!("{lengths:?}");}
Rust Reference