Rust HashMap Merging & Printing
Rust Arrays: Exercise-7 with Solution
Write a Rust program that merges two HashMaps into one and print the resulting HashMap.
Sample Solution:
Rust Code:
use std::collections::HashMap; // Import the HashMap type from the standard library
fn main() {
// Create two HashMaps to store key-value pairs
let mut first_map: HashMap<&str, i32> = HashMap::new(); // First HashMap
let mut second_map: HashMap<&str, i32> = HashMap::new(); // Second HashMap
// Insert some key-value pairs into the first HashMap
first_map.insert("a", 1);
first_map.insert("b", 2);
// Insert some key-value pairs into the second HashMap
second_map.insert("c", 3);
second_map.insert("d", 4);
// Print the original HashMaps
println!("First HashMap: {:?}", first_map);
println!("Second HashMap: {:?}", second_map);
// Merge the two HashMaps into one
let mut merged_map = first_map.clone(); // Create a clone of the first HashMap
merged_map.extend(second_map); // Extend the first HashMap with the key-value pairs from the second HashMap
// Print the resulting merged HashMap
println!("Merged HashMap: {:?}", merged_map);
}
Output:
First HashMap: {"b": 2, "a": 1} Second HashMap: {"d": 4, "c": 3} Merged HashMap: {"d": 4, "a": 1, "b": 2, "c": 3}
Explanation:
Here is a brief explanation of the above Rust code:
- use std::collections::HashMap;: Imports the "HashMap" type from the standard library, allowing us to use HashMaps in our code.
- fn main() { ... }: Defines the main function, which is the entry point of the Rust program.
- let mut first_map: HashMap<&str, i32> = HashMap::new();: This line creates an empty HashMap named 'first_map' with keys of type &str (string slice) and values of type "i32".
- let mut second_map: HashMap<&str, i32> = HashMap::new();: This line creates another empty HashMap named 'second_map' with the same key-value types as "first_map".
- first_map.insert("a", 1);: This line inserts a key-value pair into the 'first_map' HashMap, where the key is the string slice "a" and the value is the integer 1. Similar lines insert additional key-value pairs into the 'first_map' HashMap.
- second_map.insert("c", 3);: This line inserts key-value pairs into the 'second_map' HashMap in a similar manner as done for the 'first_map'.
- println!("First HashMap: {:?}", first_map);: This line prints the contents of the first HashMap before the merge operation.
- println!("Second HashMap: {:?}", second_map);: This line prints the contents of the second HashMap before the merge operation.
- let mut merged_map = first_map.clone();: This line creates a clone of the first HashMap to avoid modifying the original HashMap. The clone is stored in the variable 'merged_map'.
- merged_map.extend(second_map);: This line extends the 'merged_map' HashMap with the key-value pairs from the 'second_map' HashMap using the "extend()" method.
- println!("Merged HashMap: {:?}", merged_map);: This line prints the resulting merged HashMap after the merge operation.
Rust Code Editor:
Previous: Rust HashMap Key-Value Removal & Printing.
Next: Rust Character Frequency Counter.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/rust/collections_and_data_structures/rust-hashmaps-exercise-7.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics