Rust – HashMaps

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.
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 Rustuse std::collections::HashMap; fn main() { // initialize the HashMap// mut means we can reassign to something elselet 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 mustprintln!("{:?}",gfg );}
Output :
{"CP": "99", "Algorithms": "99",
"Data Structures": "90",
"FAANG": "97",
"Interview Preparations": "100"}
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 HashMapuse std::collections::HashMap;fn main() { // create HashMap let mut gfg=HashMap::new(); // inserting overgfg.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 gfgfor (key, val) in gfg.iter() {    println!("{} {}", key, val); }}
Output :
Algorithms 99
FAANG 97
CP 99
Interview Preparations 100
Data Structures 90
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 hashmapuse std::collections::HashMap;fn main() { // creating hashMap// mut means we can reassign gfglet 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
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 hashmapuse std::collections::HashMap;fn main() { // creating hashMap// mut means we can reassign gfglet 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
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 hashmapuse std::collections::HashMap;fn main() { // creating hashMap// mut means we can reassign gfglet 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() methodgfg.remove( & "CP");println!("len of gfg HashMap={}",gfg.len());}
Output :
len of gfg HashMap=4
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 hashmapuse std::collections::HashMap;fn main() { // creating hashMap// mut means we can reassign gfglet 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")
clintra
saurabh1990aror
Picked
Rust custom-types
Rust
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.