Rust – HashMaps
Last Updated :
15 Nov, 2021
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
use std::collections::HashMap;
fn main() {
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" );
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
use std::collections::HashMap;
fn main() {
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" );
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
use std::collections::HashMap;
fn main() {
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" );
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
use std::collections::HashMap;
fn main() {
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" );
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
use std::collections::HashMap;
fn main() {
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" );
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
use std::collections::HashMap;
fn main() {
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")
Share your thoughts in the comments
Please Login to comment...