Rust Iterator Ownership: iter, iter_mut, and into_iter
Rust iteration becomes much easier once iterator choice is connected to ownership. For a collection such as Vec<T>, the central question is whether the loop should borrow values, mutate them in place, or consume the collection. The common methods are iter(), iter_mut(), and into_iter(). Borrow with iter() iter() produces shared references: fn print_names(names: &[String]) { for name in names.iter() { println!("{name}"); } } Inside the loop, name has type &String.