Definition of Rust HashMap
Hashmap on rust is a collection that makes use of a lookup table possessing the key-value pair present within it which is used for storing and retrieving data continuously. Hashmap in rust must follow some conditions before usage within a program like it has to explicitly import the inbuild library collection. The values or data stored within the hashmap is called a hash table. Hash map in Rust is considered as a structure that can be used by including the name of the collection in the program just at the beginning of program execution.
Syntax:
The Syntax flow for Rust hashmap is as follows :
let mut name_of_map: HashMap<Type_Of_Key, Type_Of_Value> = HashMap::new();
where,
- name_of_map signifies the name of the HashMap,
- Type_Of_Key signifies the Type of key.
- Type_Of_Value signifies the value of HashMap.
How HashMap works in Rust?
- Hashmap in rust is a structure which comprises of the look-up table and data in it in form of key and value pair which will be used for storing and retrieving data continuously.
- Hashmap needs to be explicitly imported from the rust inbuilt library collection before that can be used within the program.
- The key and value present in the hashmap have their own significance in the sense that the key represents the pointer pointing to the original data in the hashmap and the value represents the corresponding value present within it.
- The entire hashmap structure imports the std::collections modules before applying the logic for implementation within the program.
- Then an instance is created once the entire library is imported for hashmap implementation.
- Hashmap functions are applied to the hashmap table for performing some required operations like insertion, removal, iter, etc.
- At last, the execution occurs with the listed operations and functions in the program.
- There are many operations for Rust HashMap execution like insertion, removal, iteration, addition, and many more.
- The key and value present within the hash table helps in one or the other way in taking decision regarding the accessibility of elements.
- HashMap uses hashing algorithm which is selected for providing resistance against HashDos attack.
- The algorithm is mostly randomly hashed and then it is used for generating some high and good quality of the seed to provide secured randomness by the host without obstructing the program from execution.
- The default hashing algorithm used is SipHash 1-3 which is completely different from the actual algorithm being implemented.
- Performance of such a hashing algorithm is different from the actual hashing algorithm with respect to competitiveness in medium-sized keys and other algorithms that will outperform the small keys with integers in the strings that will not protect any attacks such as HashDos.
- These hashing algorithms can also be replaced as per HashMap basis using a default, with_hasher, and with_capacity_and_hasher methods.
- There is a logic that exists that if the two keys are equal then their hashed values and keys must also be equal.
Examples
Let us discuss examples of Rust HashMap.
Example #1
This program illustrates the rust hash map where some subjects are created and on top of it some values are given to be inserted or added on the given collection of hash map as shown in the output.
Code:
use std::collections::HashMap;
fn main()
{
let mut subjcts = HashMap::new();
subjcts.insert("Eng", "English");
subjcts.insert("Hindi", "Hindi");
subjcts.insert("Maths", "Mathematics");
println!("{:?}", subjcts);
}
Output:
Example #2
This program illustrates the length of the string of the given hashmap as shown in the output.
Code:
use std::collections::HashMap;
fn main() {
let mut fruits = HashMap::new();
fruits.insert("Apple", "Apple");
fruits.insert("Guava", "Guava");
fruits.insert("Orange", "Orange");
fruits.insert("Kiwi", "Kiwi");
println!("Hashmap's_length_comes out to_be {:?}",fruits.len());
}
Output:
Example #3
This program illustrates the hashmap where the values corresponding to the actual values once matched gets retrieved and returns the same value as shown in the output.
Code:
use std::collections::HashMap;
fn main() {
let mut fruits = HashMap::new();
fruits.insert("Apple", "Apple");
fruits.insert("Guava", "Guava");
fruits.insert("Orange", "Orange");
fruits.insert("Kiwi", "Kiwi");
let num = fruits.get( & "Kiwi");
println!("{:?}", num);
}
Output:
Example #4
This program illustrates the hashmap where all the values present in the hash table gets iterated and displayed over the set of keys and values respectively as shown in the output.
Code:
use std::collections::HashMap;
fn main()
{
let mut fruits = HashMap::new();
fruits.insert("Apple", "Apple");
fruits.insert("Guava", "Guava");
fruits.insert("Orange", "Orange");
fruits.insert("Kiwi", "Kiwi");
for (key, val) in fruits.iter() {
println!("key_is: {} val_is: {}", key, val);
}
}
Output:
Example #5
This program illustrates to check the value whether actually, a key-value pair exists within the hashmap of collections as shown in the output.
Code:
use std::collections::HashMap;
fn main() {
let mut fruits = HashMap::new();
fruits.insert("Apple", "Apple");
fruits.insert("Guava", "Guava");
fruits.insert("Orange", "Orange");
fruits.insert("Kiwi", "Kiwi");
if fruits.contains_key( & "Orange")
{
println!("This fruits is based on hashmap_fruits.");
}
}
Output:
Example #6
This program demonstrates the hashmap where the values get from a specific hashmap with a corresponding key as shown in the output.
Code:
use std::collections::HashMap;
fn main() {
let mut fruits = HashMap::new();
fruits.insert("Guava", "Guava");
fruits.insert("Apple", "Apple");
fruits.insert("Banana", "Banana");
match fruits.get( & "Banana") {
Some(vl_0) => {
println!("Value by Banana is: {}", vl_0);
}
None => {
println!("no much to_be found.");
}
}
}
Output:
Example #7
This program demonstrates the reference for the fruits or elements present in the hashmap in the case is they don’t match and are not found but since it is found it is shown in the output.
Code:
use std::collections::HashMap;
fn main() {
let mut fruits = HashMap::new();
fruits.insert("Apple", "Apple");
fruits.insert("Mango", "Mango ");
fruits.insert("Banana", "Banana");
let reference = fruits.get( & "Mango");
println!("{:?}", reference);
}
Output:
Example #8
This program demonstrates the Rust Hashmap where the reference to the collection of fruits do not match and hence cannot provide the exact reference as shown in the output.
Code:
use std::collections::HashMap;
fn main()
{
let mut fruits = HashMap::new();
fruits.insert("Apple", "Apple");
fruits.insert("Mango", "Mango ");
fruits.insert("Banana", "Banana");
let reference = fruits.get( & "Orange");
println!("{:?}", reference);
}
Output:
Conclusion
Hashmap in Rust plays a very pivotal and powerful role as it helps in making the process of hashing with respective keys and values interactive. The overall performance with operations such as insertion, removal, and iteration with traversal is a good experience from the programmers’ point of view at the time of implementation and execution of the entire program including hashmap.
Recommended Articles
This is a guide to Rust HashMap. Here we discuss the definition, How HashMap works in Rust? and examples with code implementation. You may also have a look at the following articles to learn more –
41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses