Rust – HashMaps
The concept of HashMap is present in almost all programming languages like Java, C++, Python, it has key-value pairs and through key, we can get values of the map. Keys are unique no duplicates allowed in the key but the value can be duplicated.
1. Insertion In HashMap :
To insert a value in a HashMap in Rust follow the below steps:
- Import HashMap
- Create HashMap named gfg
- Insert the records using gfg.insert(key, value)
Syntax :
// import HashMap use std::collections::HashMap; // initialize the HashMap let mut gfg=HashMap::new(); //insert the values In HashMap gfg.insert("Data Structures","90");
Rust
// Inserting in HashMap Rust use std::collections::HashMap; fn main() { // initialize the HashMap // mut means we can reassign to something else let mut gfg=HashMap::new(); // inserting records gfg.insert( "Data Structures" , "90" ); gfg.insert( "Algorithms" , "99" ); gfg.insert( "Interview Preparations" , "100" ); gfg.insert( "FAANG" , "97" ); gfg.insert( "CP" , "99" ); // for printing all the records "{:?}" this is must println!( "{:?}" ,gfg ); } |
Output :
{"CP": "99", "Algorithms": "99", "Data Structures": "90", "FAANG": "97", "Interview Preparations": "100"}
2. Iterate over HashMap :
To iterate over a HashMap in Rust follow the below steps:
- Import HashMap
- Insert records in HashMap
- Iterate using iter() method with for loop
Syntax :
// here gfg is HashMap for (key, val) in gfg.iter() { println!("{} {}", key, val); }
Rust
// Rust Program to Iterating over HashMap // import HashMap use std::collections::HashMap; fn main() { // create HashMap let mut gfg=HashMap::new(); // inserting over gfg.insert( "Data Structures" , "90" ); gfg.insert( "Algorithms" , "99" ); gfg.insert( "Interview Preparations" , "100" ); gfg.insert( "FAANG" , "97" ); gfg.insert( "CP" , "99" ); // iterating using iter() method on gfg for (key, val) in gfg.iter() { println!( "{} {}" , key, val); } } |
Output :
Algorithms 99 FAANG 97 CP 99 Interview Preparations 100 Data Structures 90
3. Check for a Key:
To check whether a key is present in a HashMap follow the below steps:
- Import HashMap
- Insert records in HashMap
- Use contains_key(& key) to check the key present or not
Syntax :
if gfg.contains_key( & "FAANG") { println!("yes it contains the given key well done gfg"); }
Rust
// Rust program for contains Key // importing hashmap use std::collections::HashMap; fn main() { // creating hashMap // mut means we can reassign gfg let mut gfg=HashMap::new(); gfg.insert( "Data Structures" , "90" ); gfg.insert( "Algorithms" , "99" ); gfg.insert( "Interview Preparations" , "100" ); gfg.insert( "FAANG" , "97" ); gfg.insert( "CP" , "99" ); // contains_key() method to check // if key present or not if gfg.contains_key( & "FAANG" ) { println!( "yes it contains the given key well done gfg" ); } } |
Output :
yes it contains the given key well done gfg
4. Length Of HashMap :
To check for the length of HashMap in Rust follow the below steps:
- Import HashMap
- Insert records in HashMap
- Use len() method on HashMap for the length of HashMap
Syntax :
//for length of HashMap gfg.len();
Rust
// Rust program for length of HashMap // importing hashmap use std::collections::HashMap; fn main() { // creating hashMap // mut means we can reassign gfg let mut gfg=HashMap::new(); gfg.insert( "Data Structures" , "90" ); gfg.insert( "Algorithms" , "99" ); gfg.insert( "Interview Preparations" , "100" ); gfg.insert( "FAANG" , "97" ); gfg.insert( "CP" , "99" ); // length of HashMap using len() println!( "len of gfg HashMap={}" ,gfg.len()); } |
Output :
len of gfg HashMap=5
5. Remove from Hashmap :
To remove elements from a Hashmap:
- Import HashMap.
- Insert records in HashMap.
- Use the remove(key) method to remove from HashMap.
Syntax :
// length of HashMap // here gfg is HashMap gfg.remove(& "key")
Rust
// Rust program for removing from HashMap // importing hashmap use std::collections::HashMap; fn main() { // creating hashMap // mut means we can reassign gfg let mut gfg=HashMap::new(); gfg.insert( "Data Structures" , "90" ); gfg.insert( "Algorithms" , "99" ); gfg.insert( "Interview Preparations" , "100" ); gfg.insert( "FAANG" , "97" ); gfg.insert( "CP" , "99" ); // remove using remove() method gfg. remove ( & "CP" ); println!( "len of gfg HashMap={}" ,gfg.len()); } |
Output :
len of gfg HashMap=4
6. Get value using the key in HashMap :
To access values from HashMap using keys:
- Import HashMap.
- Insert records in HashMap.
- Use get( & key) for getting the value.
Syntax :
// for getting through key gfg.get( & key)
Rust
// Rust program for getting value by key // importing hashmap use std::collections::HashMap; fn main() { // creating hashMap // mut means we can reassign gfg let mut gfg=HashMap::new(); gfg.insert( "Data Structures" , "90" ); gfg.insert( "Algorithms" , "99" ); gfg.insert( "Interview Preparations" , "100" ); gfg.insert( "FAANG" , "97" ); gfg.insert( "CP" , "99" ); let value= gfg.get(& "Algorithms" ); println!( "value={:?}" ,value) } |
Output :
value=Some("99")
Please Login to comment...