How to Convert Between Collection Types in Rust

Convert Rust collection types by consuming the source with into_iter() and gathering elements into the target type using collect().

Use the into_iter() method combined with the collect() method to convert between collection types like Vec, HashMap, and String.

let vec = vec![1, 2, 3];
let hash_map: std::collections::HashMap<i32, i32> = vec.into_iter().map(|x| (x, x * 2)).collect();

The into_iter() consumes the original collection and yields its elements, while collect() gathers those elements into the target type specified by the variable annotation.