Rust Lang

Rust HashMap

A HashMap is a collection of key-value pairs. HashMaps are a mutual concept in most programming languages. You may often find them under different naming such as a hash, dictionaries, maps, objects, associative arrays, etc.

A HashMap contains a key and its corresponding value. In Rust, a hashing function determines how the key and value pairs are allocated in the memory managed.

Let us break down how HashMaps work and how to use them in our Rust programs.

HashMap module

To use a HashMap, we need to imp0rt the HashMap module from the Rust standard collections library.

An import statement is as shown:

use std::collections::HashMap;

Once imported, we can use it to work with HashMaps in Rust.

Rust Create HashMap

We can define a new empty hash map using the new method. An example is as shown below:

fnmain() {
letmutuser_info = HashMap::new();

The above creates a new empty hash map that stores user information. To add elements to the hash map, we can use the insert method as shown in the example below:

fnmain() {
    let mut user_info = HashMap::new();
user_info.insert("Russell ", "California");
user_info.insert("Donna", "Wyoming");
user_info.insert("Franklin", "Colorado");
user_info.insert("Elise", "Pennysylania");
user_info.insert("Mason", "Virgina");

To print the hash map, we can use the println! method with the debug trait as shown:

println!("{:?}", user_info);

This should return the complete hash map information.

{"Mason": "Virgina", "Donna": "Wyoming", "Franklin": "Colorado", "Elise": "Pennysylania", "Russell ": "California"}

Rust Iterate Hash Map.

We can iterate over a hash map using the iter method. An example illustration is as shown below:

for (key, value) in user_info.iter() {

println!("{}: {}", key, value);


The iter method will return a set of keys and their corresponding values. An example output is as shown:

Donna: Wyoming

Russell : California

Mason: Virgina

Franklin: Colorado

Elise: Pennysylania

Rust Check If Key Exists

To check if a key exists within a hash map, you can use the contains_key() method. An example is as shown:

println!("{}", user_info.contains_key("Elise"));

The function takes the key to lookup as an argument and returns a Boolean true if the key exists in the hash map.

Rust Retrieve Values

You can retrieve the values from a map using the get method. The function takes the key you wish to access as the argument and returns the corresponding value.

An example is as shown:

println!("{:?}", user_info.get("Mason"));

if the specified key exists in the collection, Rust will return the associated value and a None if the key is not found.

Rust Update HashMap

You can update a key-value pair in a hash map by passing a unique value to an existing key. An example is as shown:

user_info.insert("Mason", "Texas");

println!("{:?}", user_info.get("Mason"));

Since the key “Mason” already exists in the hash map, Rust automatically substitutes its value with the new one.


Rust Remove Key-Value

We can remove a key-value pair from a hash map using the remove method. The method takes the key we wish to remove as the argument.

An example is as shown:


Final Thoughts

HashMaps play a critical role in providing diversity and extensibility for your programs. Keep practicing!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list