Updated June 2, 2023
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 import the inbuild library collection explicitly. The values or data stored within the hashmap is called a hash table. Hash map in Rust is considered 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 comprising the look-up table and data in key and value pair, which will be used for storing and retrieving data continuously.
- To use HashMap within a program, you need to explicitly import it from the Rust built-in library collection.
- 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 to perform 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 deciding the accessibility of elements.
- HashMap uses hashing algorithm, which is selected to provide resistance against HashDos attack.
- The algorithm randomly hashes the data and generates a high-quality seed. The host utilizes this seed to provide secure randomness without obstructing the program’s execution.
- The default hashing algorithm used in HashMap is SipHash 1-3, which differs completely from the implemented algorithm.
- 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.
- You can replace the hashing algorithms on a per HashMap basis by using the default, with_hasher, and with_capacity_and_hasher methods.
- A logic exists that if the two keys are equal, their hashed values and keys must also be equal.
Examples
Let us discuss examples of Rust HashMap.
Example #1
This program showcases the usage of a Rust hash map. It generates a collection of subjects and inserts values into the hash map. The resulting output displays the hash map with the added values.
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 string length 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 how the hashmap retrieves and returns the values that correspond to the actual values once they match. The output demonstrates this behavior.
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 iterating and displaying all the values present in the hash table 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 whether 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 how the hashmap references the fruits or elements. If the program does not find a match, it displays the corresponding output. However, in this case, since a match is found, it shows 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 fruit collection does not match and hence cannot provide the exact reference 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
We hope that this EDUCBA information on “Rust HashMap” was beneficial to you. You can view EDUCBA’s recommended articles for more information.