1 00:00:05,590 --> 00:00:10,120 Sets are a collection we can use for quickly checking if a value exists. 2 00:00:10,480 --> 00:00:13,990 A set can never contain multiple copies of the same value. 3 00:00:14,080 --> 00:00:21,160 Maps and sets are different, but when you look behind the scenes, a set is similar to a map, except 4 00:00:21,250 --> 00:00:27,490 a set is only going to have keys and not a key pair value like a map. 5 00:00:27,790 --> 00:00:31,300 So like maps though, there are two sets. 6 00:00:31,300 --> 00:00:35,050 Rust, give us a hash set and a B tree set. 7 00:00:35,140 --> 00:00:39,490 And again, hash set and B tree set are similar. 8 00:00:39,490 --> 00:00:43,330 So I'm only going to show you how we can use a hash set. 9 00:00:43,660 --> 00:00:44,860 So let's start out. 10 00:00:45,700 --> 00:00:51,520 By adding the hash set into our scope. 11 00:00:54,640 --> 00:00:56,260 And now we can come down here. 12 00:00:57,530 --> 00:01:02,570 We can create a mutable hash set with the new keyword. 13 00:01:08,310 --> 00:01:14,220 And like a hash map, we can have link to return. 14 00:01:14,220 --> 00:01:25,680 How many values are in our hash set as well as is empty to return a boolean on if our set is empty or 15 00:01:25,680 --> 00:01:26,190 not. 16 00:01:26,700 --> 00:01:35,850 So just like a hash map, let's begin adding in some values with the insert keyword and we'll add in 17 00:01:35,850 --> 00:01:36,840 our values. 18 00:01:36,990 --> 00:01:38,700 And in this case. 19 00:01:40,010 --> 00:01:45,470 Just add in one, two, three and four. 20 00:01:50,020 --> 00:01:58,450 And for so to print this out, we can use what's known as an iterator and we'll be able to talk a little 21 00:01:58,450 --> 00:02:02,200 bit more about readers a little bit later in this course. 22 00:02:02,290 --> 00:02:04,840 But we can do for x in. 23 00:02:06,710 --> 00:02:08,780 H.S. dot hitter. 24 00:02:09,800 --> 00:02:12,290 Now we just want to print line. 25 00:02:14,930 --> 00:02:19,070 Ed and X. 26 00:02:22,750 --> 00:02:24,250 So now if we run this. 27 00:02:25,700 --> 00:02:30,020 See our iterator and I'm going to get rid of this. 28 00:02:30,320 --> 00:02:31,820 And because that will bother me. 29 00:02:32,720 --> 00:02:36,680 But we have one, two, three and four, which is what we added in. 30 00:02:36,680 --> 00:02:37,310 So. 31 00:02:39,870 --> 00:02:49,260 To remove a value like we did in a vector, we can just say, Hey, we want to remove a reference to 32 00:02:49,260 --> 00:02:55,560 the two, and then up here we'll actually cut and paste this. 33 00:02:56,500 --> 00:02:57,610 Run it again. 34 00:02:58,300 --> 00:03:01,740 And now we only have one, three and four. 35 00:03:01,750 --> 00:03:05,470 But I'm going to comment that out just to keep our values in there. 36 00:03:07,030 --> 00:03:11,800 So now I'm going to create another hash set called HS2. 37 00:03:11,830 --> 00:03:16,330 Just for hash it two and have it be new. 38 00:03:18,040 --> 00:03:20,050 And then I'm going to come down here. 39 00:03:20,050 --> 00:03:20,710 Up here. 40 00:03:20,710 --> 00:03:21,730 And. 41 00:03:23,260 --> 00:03:29,050 Let's say one, three, five, one, three. 42 00:03:30,470 --> 00:03:32,990 Five and seven. 43 00:03:33,990 --> 00:03:40,080 Then obviously we need to change these to set, too. 44 00:03:41,160 --> 00:03:42,780 So now we have another hash set. 45 00:03:43,570 --> 00:03:45,890 With the values one, three and five in it. 46 00:03:46,970 --> 00:03:52,250 So now with sets, we are actually given some new methods. 47 00:03:52,520 --> 00:03:57,290 So the the first one we're going to talk about is intersection. 48 00:03:57,380 --> 00:04:04,240 And when you check intersection within two sets, you're going to see which values are in both sets. 49 00:04:04,250 --> 00:04:08,810 So the first way we can do this is say four X in hash set. 50 00:04:10,110 --> 00:04:18,690 Intersection and then pass in a reference to the second set and now we can print line. 51 00:04:20,670 --> 00:04:24,000 And we're going to say intersection. 52 00:04:26,960 --> 00:04:27,620 X. 53 00:04:31,300 --> 00:04:38,380 And so we see we have one and three in both sets and intersection is. 54 00:04:39,890 --> 00:04:43,580 Has printed out one in three between the two sets. 55 00:04:43,790 --> 00:04:53,180 So another way that we can do this, a more shorthand way, is we can actually say let intersection. 56 00:04:54,170 --> 00:04:58,820 Equal to a reference to the first set. 57 00:04:59,630 --> 00:05:00,170 The. 58 00:05:01,720 --> 00:05:10,390 Binary bitwise and operator, so the ampersand sign and then a reference to the second set. 59 00:05:10,840 --> 00:05:14,950 So now intersection is going to contain. 60 00:05:15,310 --> 00:05:19,120 It's going to be a set that's going to contain. 61 00:05:20,130 --> 00:05:22,050 The values that are in both sets. 62 00:05:22,440 --> 00:05:32,580 So let's print that out for X, an intersection print line, and then we're going to say shorthand way. 63 00:05:36,970 --> 00:05:42,670 And now X and we expect this to still be the same results of one and three. 64 00:05:43,850 --> 00:05:53,990 Which is what we got so that this is a shorthand, way short shorthand intersection using the bitwise. 65 00:05:54,910 --> 00:06:02,400 The excuse me, the binary bitwise and person operator reader. 66 00:06:05,940 --> 00:06:15,000 Another one that we have is union and unions going to return values that are in either the first hash 67 00:06:15,000 --> 00:06:18,740 set or the second hash set? 68 00:06:18,750 --> 00:06:26,390 So I'm only going to show you the shorthand way because otherwise it's going to be the same way above. 69 00:06:26,400 --> 00:06:34,050 Instead of it being hash set, then method called intersection, our method call would be union. 70 00:06:34,050 --> 00:06:44,520 So let's do our shorthand way and say a reference to the hash set with the binary or. 71 00:06:46,600 --> 00:06:49,240 Operator and then set. 72 00:06:50,260 --> 00:06:50,910 Two. 73 00:06:50,920 --> 00:06:58,480 So now union is going to be a set that's going to contain values that are that are in either of the 74 00:06:58,510 --> 00:06:59,920 two hash sets. 75 00:07:00,400 --> 00:07:02,230 So let's print this out. 76 00:07:03,950 --> 00:07:10,900 To see to make sure that we got the correct values union. 77 00:07:15,850 --> 00:07:17,140 And then X. 78 00:07:19,260 --> 00:07:22,410 And for union in. 79 00:07:22,440 --> 00:07:23,190 There we go. 80 00:07:24,830 --> 00:07:28,070 And now if we run this, we have union. 81 00:07:28,070 --> 00:07:31,820 One, two, three, four, five and seven. 82 00:07:32,560 --> 00:07:40,330 So we can see that we didn't have duplicate values of one or three, but we have all the values occurring 83 00:07:40,330 --> 00:07:43,570 once between the two sets. 84 00:07:45,100 --> 00:07:49,600 And then lastly, we can use we can do the difference. 85 00:07:49,600 --> 00:07:56,500 So likewise difference would be called like this, but we're going to do the shorthand way and say let 86 00:07:56,500 --> 00:08:00,880 diff equals a reference to the first hash set minus. 87 00:08:01,090 --> 00:08:09,010 So the difference between the second hash set and now we can print this out. 88 00:08:12,960 --> 00:08:16,950 And see what the difference is and we'll say diff. 89 00:08:20,900 --> 00:08:21,920 An X. 90 00:08:23,330 --> 00:08:30,020 And so with the difference, we want to see what values are in the first hash set, but not the second 91 00:08:30,020 --> 00:08:30,410 hash. 92 00:08:31,040 --> 00:08:34,580 So if we run this, we see that we have two and four. 93 00:08:34,880 --> 00:08:39,810 And that would be correct because we have two, four, and then the second hash. 94 00:08:39,830 --> 00:08:42,170 Yet we do not have two and four. 95 00:08:43,550 --> 00:08:48,530 So there are more methods we can use on a set, but these are some of the main ones. 96 00:08:48,530 --> 00:08:53,240 And as you can see, we can do a lot of logic handling very quickly on sets. 97 00:08:54,430 --> 00:09:00,040 Again, these are just some of the collections we have available to us in Rust, so feel free to check 98 00:09:00,040 --> 00:09:01,570 out more collections. 99 00:09:01,720 --> 00:09:06,970 But again, later on down the road, I do plan to show how we can create some of the data structures 100 00:09:06,970 --> 00:09:13,600 available to use in rest so that we can continue to learn and become more proficient in our rest journey.