Rust, Software Development

Rust – Check if key exists in HashMap Examples

This post shows how to check if a key exists in a HashMap in Rust. As we know, Rust has the HashMap struct, which is part of its Collections library that we can use to store key-value pairs.

Rust HashMap – Strings as Keys

The HashMap struct uses generics. With generics, we can define the data type of the keys and their values. Consider the following codes.

We have an instance of HashMap whose keys and values are of type String. To check if one of our keys is in the HashMap instance, consider the following codes. Super easy, right?

The codes generate the following output.

Numeric Keys Example

From String keys, let us explore numeric values as keys and spice things up a little more. Consider these initial codes. Relax, they are very similar to the previous one. The only difference now is we are using i32 as numeric keys!

At line 4, we specify the first formal type parameter of the generics to be i32. Next, from lines 6 to 8, we insert key-value pairs to the HashMap instance – the keys are numeric values, and the values are of String type.

Then, we check if some numeric keys exist in our HashMap instance – see lines 12, 13, and 14. Notice we use the ampersand symbol before the value (or variables if we stored the values in them) to pass the reference instead of the actual value. For numeric variables, we would code something like the following.

Numeric keys are still easy! We did not even break a sweat! Now, moving on – we will use our struct instance as keys. Are you excited?

Check Keys As Struct Instances

This section of the post required a little preparation. We need more codes here than in the previous parts. So, hang on tight! Since we are going to use instances of our struct, we need to prep it up.

First, we start with a simple struct design. So, we have a Person struct with person_id and person_name fields. Simple enough!

We need to use the inline implementation of Debug, Eq, and Hash traits using the derive directive. The directive implements the traits for our struct but not quite for the Eq trait.

Although we specify the Eq trait in the directive, we still need explicitly to implement the PartialEq trait. Why? There is a long discussion among Rust enthusiasts about that, but I would skip it for this post.

The boolean expression is important because it will match a single key (which are Person instances) in the HashMap instance.

Now, we are almost complete. Let us see the codes in the main function. First, we create a HashMap instance whose keys are instances of Person struct, and values are of String type.

Next, we fill it with Person instances.

To check if these instances are indeed in our HashMap, we print out its content.

Then, we create new instances of Person struct to serve as test keys and retrieve their corresponding String values.

Notice that we created a Person instance not found in our MashMap instance. We will use it for negative testing.

Now the testing part, we have these Rust codes to check if the test keys exist in our HashMap.

When we run all the codes, we will get the following output.

These are just basic, and we can still come up with other types of keys!

This post is now part of the Rust Programming Language For Beginners Tutorial.

Got comments, or suggestions? Please visit our Facebook page!

You Might Also Like