deconstruct hashmap into vecs rust
To deconstruct a HashMap into Vecs in Rust, you can use the into_iter()
method to iterate over the HashMap and convert it into a Vec of key-value pairs. Then, you can use the unzip()
method to separate the keys and values into separate Vecs.
Here's an example code snippet:
use std::collections::HashMap;
fn main() {
let mut hashmap = HashMap::new();
hashmap.insert("key1", "value1");
hashmap.insert("key2", "value2");
hashmap.insert("key3", "value3");
let (keys, values): (Vec<_>, Vec<_>) = hashmap.into_iter().unzip();
println!("Keys: {:?}", keys);
println!("Values: {:?}", values);
}
This code creates a HashMap, inserts some key-value pairs, and then deconstructs the HashMap into separate Vecs for keys and values using the unzip()
method. Finally, it prints the keys and values.
I hope this helps! Let me know if you have any further questions.