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")
Similar Reads
Kotlin Hashmap In Kotlin, a HashMap is a collection that stores key-value pairs, where each key must be unique, but values can be duplicated. It is a hash table based implementation of the MutableMap interface. Map keys are unique and the map holds only one value for each key. It is represented as HashMap<key,
7 min read
Kotlin Hashmap In Kotlin, a HashMap is a collection that stores key-value pairs, where each key must be unique, but values can be duplicated. It is a hash table based implementation of the MutableMap interface. Map keys are unique and the map holds only one value for each key. It is represented as HashMap<key,
7 min read
Rust - Array An Array in Rust programming is a fixed-sized collection of elements denoted by [T; N] where T is the element type and N is the compile-time constant size of the array. We can create an array in 2 different ways: Simply a list with each element [a, b, c].Repeat expression [N, X]. Â This will create a
5 min read
How to use Hashmap in TypeScript ? In TypeScript, One can use the built-in Map data structure to implement a hashmap. A Map can be used to store key-value pairs where keys can be of any type. Syntax:// Create a new empty hashmaplet hashmap: Map<KeyType, ValueType> = new Map();// Add key-value pairs to the hashmaphashmap.set(key
2 min read
Rust - File Hierarchy of Modules In the Rust programming language, modules are used to organize code into logical units and control their visibility (i.e., whether they can be accessed from other parts of the code). Modules can be nested within other modules, and this can help to create a hierarchical structure for organizing your
4 min read
Rust - File Hierarchy of Modules In the Rust programming language, modules are used to organize code into logical units and control their visibility (i.e., whether they can be accessed from other parts of the code). Modules can be nested within other modules, and this can help to create a hierarchical structure for organizing your
4 min read