Rust HashMap Key-Value Removal & Printing
Rust Arrays: Exercise-6 with Solution
Write a Rust program to remove a key-value pair from a HashMap and print the HashMap after removal.
Sample Solution:
Rust Code:
use std::collections::HashMap; // Import the HashMap type from the standard library
fn main() {
// Create a HashMap to store key-value pairs
let mut my_map: HashMap<&str, i32> = HashMap::new(); // Key: &str (string slice), Value: i32
// Insert some key-value pairs into the HashMap
my_map.insert("a", 1);
my_map.insert("b", 2);
my_map.insert("c", 3);
// Print the original HashMap
println!("Original HashMap: {:?}", my_map);
// Define the key to remove from the HashMap
let key_to_remove = "b";
// Remove the key-value pair from the HashMap
let removed_value = my_map.remove(key_to_remove);
// Print the HashMap after removal
match removed_value {
Some(_) => println!("HashMap after removing key '{}': {:?}", key_to_remove, my_map), // If the key existed and was removed, print the updated HashMap
None => println!("Key '{}' does not exist in the HashMap.", key_to_remove), // If the key did not exist, print a message
}
}
Output:
Original HashMap: {"c": 3, "a": 1, "b": 2} HashMap after removing key 'b': {"c": 3, "a": 1}
Explanation:
Here is a brief explanation of the above Rust code:
- use std::collections::HashMap;: This line imports the "HashMap" type from the standard library, allowing us to use HashMaps in our code.
- fn main() { ... }: This line defines the main function, which is the entry point of the Rust program.
- let mut my_map: HashMap<&str, i32> = HashMap::new();: This line creates an empty HashMap named 'my_map' with keys of type &str (string slice) and values of type i32.
- my_map.insert("a", 1);: This line inserts a key-value pair into the 'my_map' HashMap, where the key is the string slice "a" and the value is the integer 1. Similar lines insert additional key-value pairs.
- println!("Original HashMap: {:?}", my_map);: This line prints the original HashMap before any removals are made.
- let key_to_remove = "b";: This line defines the key that we want to remove from the HashMap.
- let removed_value = my_map.remove(key_to_remove);: This line removes the key-value pair associated with the specified key from the HashMap using the" remove()" method and stores the removed value, if any, in the 'removed_value' variable.
- match removed_value { ... }: This line starts a "match" statement to handle the result of removing the key-value pair:
- If the key existed and was removed (Some(_)), it prints the updated HashMap after removal.
- If the key did not exist (None), it prints a message indicating that the key does not exist in the HashMap.
Rust Code Editor:
Previous: Rust HashMap Key-Value Update & Printing.
Next: Rust HashMap Merging & Printing.
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://www.w3resource.com/rust/collections_and_data_structures/rust-hashmaps-exercise-6.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics