1 00:00:05,840 --> 00:00:09,140 A map is a collection of key pair values. 2 00:00:09,380 --> 00:00:11,420 No entries can have the same key. 3 00:00:11,420 --> 00:00:16,520 And when you want to look up an entry, you just need to know the key for a quick lookup. 4 00:00:17,000 --> 00:00:23,570 Rust gives us two types of maps hash map and a B tree map, also known as a binary tree map. 5 00:00:24,660 --> 00:00:26,550 These two maps are very similar. 6 00:00:26,580 --> 00:00:31,470 The key difference is how the two maps keep entries arranged for their lookup. 7 00:00:31,710 --> 00:00:38,190 Hash maps are stored in a single heap allocated table, whereas tree maps are stored in a tree like 8 00:00:38,190 --> 00:00:43,200 structure growing from the top down into many leaves, also called nodes. 9 00:00:44,490 --> 00:00:51,510 I don't want to get too into the data structure set up behind these, but I do want to point out that 10 00:00:51,510 --> 00:00:58,740 I eventually plan to add a data structures section to this course to where we can see how data structures 11 00:00:58,740 --> 00:01:02,850 are set up and then also improve in our rest skills. 12 00:01:02,940 --> 00:01:10,470 But again, these two maps have very similar methods, so I am only going to create and use a hash map, 13 00:01:10,470 --> 00:01:13,110 but it is similar to a B tree map. 14 00:01:13,320 --> 00:01:18,870 So the first thing we need to do is bring a hash map into our scope and it's going to be in standard 15 00:01:18,870 --> 00:01:22,110 collections, hash map, as was the binary heap. 16 00:01:23,760 --> 00:01:32,410 And now we can create a mutable hash map using hash map loop. 17 00:01:33,390 --> 00:01:45,030 So like a B binary heap, did we have the line method for length and the is empty method as well, which 18 00:01:45,030 --> 00:01:51,660 is remember going to return a boolean on if there are values or not in there. 19 00:01:52,760 --> 00:02:00,140 So let's add a couple of values using the insert method, which as you can see, it's going to accept 20 00:02:00,140 --> 00:02:05,750 two parameters the key and the value designated by K and V. 21 00:02:05,750 --> 00:02:10,670 And so we can both see that they are generic types. 22 00:02:11,000 --> 00:02:16,070 So let's add in a key of one and then also a value of one. 23 00:02:17,060 --> 00:02:21,560 So then we can add a couple more in and we'll say insert five. 24 00:02:23,630 --> 00:02:24,530 And to. 25 00:02:26,540 --> 00:02:34,700 And then we'll say insert and we'll give a key of 30 and a value of three. 26 00:02:35,810 --> 00:02:43,040 So now let's print out our hash map just to kind of see how it is structured. 27 00:02:49,030 --> 00:02:53,200 Cargo run and we can see that we have 1 to 1. 28 00:02:53,200 --> 00:02:59,530 So key one value one, key five value two and then key 30, value three. 29 00:03:00,560 --> 00:03:07,270 So now earlier I mentioned that you cannot have multiple values mapped to a key. 30 00:03:07,280 --> 00:03:13,280 So what happens if I say at key 30, I want you to have. 31 00:03:14,480 --> 00:03:23,580 The value for because we can see that the ID does not give us any error codes, so it looks valid. 32 00:03:23,600 --> 00:03:32,570 Well, what's actually going to happen here is the key is going to update 33 00:03:34,910 --> 00:03:41,000 the old value of three to the new value of four. 34 00:03:41,310 --> 00:03:55,050 It's also going to return the old value so we can actually say let hold equals and then to the insert. 35 00:03:55,070 --> 00:04:03,860 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 36 00:04:03,860 --> 00:04:04,430 value. 37 00:04:05,610 --> 00:04:13,200 And now if we run this, we see that the key of 30 updated to our new value of four and then. 38 00:04:14,140 --> 00:04:14,920 We got. 39 00:04:15,610 --> 00:04:19,390 The old variable was assigned some three. 40 00:04:19,390 --> 00:04:22,960 So it's also going to return an option of. 41 00:04:24,820 --> 00:04:25,570 So great. 42 00:04:25,570 --> 00:04:29,500 So we're able to actually update values using the insert. 43 00:04:29,500 --> 00:04:38,560 That way we don't generate any bugs trying to act to insert a value at a key that already exists. 44 00:04:39,790 --> 00:04:52,630 So we can check to see if a key exists using the contains key method and that looks like this and we 45 00:04:52,630 --> 00:04:56,740 can see that it is looking for a reference to a key value. 46 00:04:56,740 --> 00:05:00,760 So in this case we'll say a reference to five. 47 00:05:00,760 --> 00:05:06,220 And if we run this, we would expect true to be printed out, which is what we got. 48 00:05:06,220 --> 00:05:13,180 So if we reference a key that does not exist, such as eight false is returned. 49 00:05:13,180 --> 00:05:14,020 So that's great. 50 00:05:14,020 --> 00:05:18,850 So now we can check to see if keys exist inside of our hash map. 51 00:05:19,930 --> 00:05:21,370 But how about getting. 52 00:05:22,890 --> 00:05:24,280 Values back. 53 00:05:24,570 --> 00:05:26,250 So we can say. 54 00:05:27,330 --> 00:05:28,200 Brit. 55 00:05:28,740 --> 00:05:38,820 And then we want to print out the value of the key and we'll say key five again. 56 00:05:39,060 --> 00:05:41,730 So when we run this, we expect. 57 00:05:43,850 --> 00:05:46,650 Two to be returned and we get some too. 58 00:05:46,670 --> 00:05:53,480 So we are returned and option type T. 59 00:05:54,950 --> 00:06:01,240 So that that means that if that key does not exist, then none would be returned. 60 00:06:01,250 --> 00:06:07,340 Otherwise we are going to return some t if that key does exist. 61 00:06:07,670 --> 00:06:11,350 So now we have two ways that we can remove values. 62 00:06:11,360 --> 00:06:18,370 The first way is remove and we're going to pass in the key that we want removed. 63 00:06:18,380 --> 00:06:22,040 So in this case, we'll pass in the key of one. 64 00:06:25,130 --> 00:06:31,190 If we now print out, ah, I'm just going to copy this. 65 00:06:31,670 --> 00:06:39,290 If we now print out our hash map, we expect the 1 to 1 to be gone. 66 00:06:40,300 --> 00:06:41,260 Which as you can see. 67 00:06:41,260 --> 00:06:43,120 So it's up here when we create it. 68 00:06:43,120 --> 00:06:47,410 And now down here are key of one and value one is gone. 69 00:06:47,650 --> 00:06:50,200 But this also returns. 70 00:06:52,080 --> 00:06:53,320 Value for us. 71 00:06:53,320 --> 00:06:59,290 So if we run it this way, then we see we get some one return. 72 00:06:59,290 --> 00:07:03,130 So it does return a value if we decide to do it that way. 73 00:07:04,120 --> 00:07:10,210 Now, another way we can do it is we can say remove entry. 74 00:07:10,210 --> 00:07:12,880 So we can also. 75 00:07:13,970 --> 00:07:19,040 You also get a returned value from this, but it is a returned paper value. 76 00:07:19,040 --> 00:07:26,300 So remove entry is going to actually return the key and the value. 77 00:07:26,720 --> 00:07:37,730 So let's remove the key of five and make sure that the key of five is returned as well as the value 78 00:07:37,730 --> 00:07:39,710 so we can see that we are returned. 79 00:07:39,710 --> 00:07:44,930 The key of five and the value of two, which is what we initially inserted in. 80 00:07:45,990 --> 00:07:53,790 So the difference between remove entry and remove is that remove entry is also going to return the key 81 00:07:53,790 --> 00:07:58,770 alongside the value, whereas remove is only going to return the value. 82 00:07:59,160 --> 00:08:06,390 And then if we quickly want to clear out our hash map, we can use clear and then to verify that this 83 00:08:06,390 --> 00:08:16,350 was cleared out, we can use our method is empty and since we ran clear, we would expect this to return 84 00:08:16,350 --> 00:08:18,570 true, which is what we got. 85 00:08:19,020 --> 00:08:22,860 So these are just some of the methods associated with the hash map. 86 00:08:22,860 --> 00:08:26,760 Again, a B tree map and a hash map are extremely similar. 87 00:08:26,760 --> 00:08:33,510 So I encourage you to create a B tree map and then just basically do what we did here just to make sure 88 00:08:33,510 --> 00:08:35,010 everything works the same. 89 00:08:35,010 --> 00:08:39,990 And that you are also familiar with how a B tree map works.