A map is a collection of key pair values.
No entries can have the same key.
And when you want to look up an entry, you just need to know the key for a quick lookup.
Rust gives us two types of maps hash map and a B tree map, also known as a binary tree map.
These two maps are very similar.
The key difference is how the two maps keep entries arranged for their lookup.
Hash maps are stored in a single heap allocated table, whereas tree maps are stored in a tree like
structure growing from the top down into many leaves, also called nodes.
I don't want to get too into the data structure set up behind these, but I do want to point out that
I eventually plan to add a data structures section to this course to where we can see how data structures
are set up and then also improve in our rest skills.
But again, these two maps have very similar methods, so I am only going to create and use a hash map,
but it is similar to a B tree map.
So the first thing we need to do is bring a hash map into our scope and it's going to be in standard
collections, hash map, as was the binary heap.
And now we can create a mutable hash map using hash map loop.
So like a B binary heap, did we have the line method for length and the is empty method as well, which
is remember going to return a boolean on if there are values or not in there.
So let's add a couple of values using the insert method, which as you can see, it's going to accept
two parameters the key and the value designated by K and V.
And so we can both see that they are generic types.
So let's add in a key of one and then also a value of one.
So then we can add a couple more in and we'll say insert five.
And to.
And then we'll say insert and we'll give a key of 30 and a value of three.
So now let's print out our hash map just to kind of see how it is structured.
Cargo run and we can see that we have 1 to 1.
So key one value one, key five value two and then key 30, value three.
So now earlier I mentioned that you cannot have multiple values mapped to a key.
So what happens if I say at key 30, I want you to have.
The value for because we can see that the ID does not give us any error codes, so it looks valid.
Well, what's actually going to happen here is the key is going to update
the old value of three to the new value of four.
It's also going to return the old value so we can actually say let hold equals and then to the insert.
So now if we come down here and we try to print this out and we'll say we want to print out the old
value.
And now if we run this, we see that the key of 30 updated to our new value of four and then.
We got.
The old variable was assigned some three.
So it's also going to return an option of.
So great.
So we're able to actually update values using the insert.
That way we don't generate any bugs trying to act to insert a value at a key that already exists.
So we can check to see if a key exists using the contains key method and that looks like this and we
can see that it is looking for a reference to a key value.
So in this case we'll say a reference to five.
And if we run this, we would expect true to be printed out, which is what we got.
So if we reference a key that does not exist, such as eight false is returned.
So that's great.
So now we can check to see if keys exist inside of our hash map.
But how about getting.
Values back.
So we can say.
Brit.
And then we want to print out the value of the key and we'll say key five again.
So when we run this, we expect.
Two to be returned and we get some too.
So we are returned and option type T.
So that that means that if that key does not exist, then none would be returned.
Otherwise we are going to return some t if that key does exist.
So now we have two ways that we can remove values.
The first way is remove and we're going to pass in the key that we want removed.
So in this case, we'll pass in the key of one.
If we now print out, ah, I'm just going to copy this.
If we now print out our hash map, we expect the 1 to 1 to be gone.
Which as you can see.
So it's up here when we create it.
And now down here are key of one and value one is gone.
But this also returns.
Value for us.
So if we run it this way, then we see we get some one return.
So it does return a value if we decide to do it that way.
Now, another way we can do it is we can say remove entry.
So we can also.
You also get a returned value from this, but it is a returned paper value.
So remove entry is going to actually return the key and the value.
So let's remove the key of five and make sure that the key of five is returned as well as the value
so we can see that we are returned.
The key of five and the value of two, which is what we initially inserted in.
So the difference between remove entry and remove is that remove entry is also going to return the key
alongside the value, whereas remove is only going to return the value.
And then if we quickly want to clear out our hash map, we can use clear and then to verify that this
was cleared out, we can use our method is empty and since we ran clear, we would expect this to return
true, which is what we got.
So these are just some of the methods associated with the hash map.
Again, a B tree map and a hash map are extremely similar.
So I encourage you to create a B tree map and then just basically do what we did here just to make sure
everything works the same.
And that you are also familiar with how a B tree map works.