Masai Mahapa

30 days of Rust - Day Seven - HashMaps

30 days of rust - day Six Yesterday I learnt of Vectors which are amazing for storing data next to each other in memory such as a shopping cart. This is cool for most cases, the problem comes when trying to retrieve an item and you don't know it's index. In order to find shoes in a shopping cart, we would have to basically loop over all of the items in the cart until we find shoes. It works, but can be computationally expensive as we have to check each and every item on the list. We'll talk about computational complexity some other day.

Day 7- HashMaps

A Hashmap is a data type which stores a mapping of a Key to a Value. If you come from another programming language you would also know it as a Dictionary from python.

Hash maps make it very easy to access data with just the key. Similar to how you would get an item in an array or Vector with its index.

Creating a Hash Map

We firstly need to import it as it's not part of Rust's prelude (automatic imports).

use std::collections::HashMap;
 
fn main() {
   let mut shopping_cart = HashMap::new();
   shopping_cart.insert(String::from("shoes"), 1500);
   shopping_cart.insert(String::from("Sgarru-Collection"), 1000);
}

Similar to what we saw in Vectors, HashMaps require all they keys to be of the same type. In our case String and all the values be of the same type too, i32 in our case.

Getting values from a HashMap

We need to use a key in order to get the value. For this we use the get method;

   let sgarru = String::from("Sgarru-Collection");
   let item = shopping_cart.get(&sgarru);

The .get method returns an Option, because there is a chance that we may be trying to access an item which is not within our cart, giving us a None value.

looping over a Hash Map

Let's say we want to show a custom all the item which are in their current shopping cart. We'd have to loop through all the items, and hashmaps make it easy;

 for (key, value) in &shopping_cart {
       println!("{}: {}", key, value);
   }

would output;

shoes: 1500
Sgarru-Collection: 1000

Updating values

We can change the values of the items contained in our hashmap by using the same insert method before. We just have to use the key of an existing item, otherwise it will add a new key-value pair.

Quick example, let's say Sgarru makes a 30% sale on their Collection. We can change the value from 1000 to 700 as follow;

// initial value
shopping_cart.insert(String::from("Sgarru-Collection"), 1000);
// updated value
shopping_cart.insert(String::from("Sgarru-Collection"), 700);

Conclusion

This data type is very simple, yet very powerful. Hash Maps allow us to retrieve data without knowing their index, by simply using their Key. It is worth having in your toolkit.

Please let me know what you would use a Hashmap for.

I really appreciate you having to take time out of your day to read these blog posts. If you've only just got here, please read from my day one and see how far we have come.

Share