Sets are a collection we can use for quickly checking if a value exists.
A set can never contain multiple copies of the same value.
Maps and sets are different, but when you look behind the scenes, a set is similar to a map, except
a set is only going to have keys and not a key pair value like a map.
So like maps though, there are two sets.
Rust, give us a hash set and a B tree set.
And again, hash set and B tree set are similar.
So I'm only going to show you how we can use a hash set.
So let's start out.
By adding the hash set into our scope.
And now we can come down here.
We can create a mutable hash set with the new keyword.
And like a hash map, we can have link to return.
How many values are in our hash set as well as is empty to return a boolean on if our set is empty or
not.
So just like a hash map, let's begin adding in some values with the insert keyword and we'll add in
our values.
And in this case.
Just add in one, two, three and four.
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
bit more about readers a little bit later in this course.
But we can do for x in.
H.S. dot hitter.
Now we just want to print line.
Ed and X.
So now if we run this.
See our iterator and I'm going to get rid of this.
And because that will bother me.
But we have one, two, three and four, which is what we added in.
So.
To remove a value like we did in a vector, we can just say, Hey, we want to remove a reference to
the two, and then up here we'll actually cut and paste this.
Run it again.
And now we only have one, three and four.
But I'm going to comment that out just to keep our values in there.
So now I'm going to create another hash set called HS2.
Just for hash it two and have it be new.
And then I'm going to come down here.
Up here.
And.
Let's say one, three, five, one, three.
Five and seven.
Then obviously we need to change these to set, too.
So now we have another hash set.
With the values one, three and five in it.
So now with sets, we are actually given some new methods.
So the the first one we're going to talk about is intersection.
And when you check intersection within two sets, you're going to see which values are in both sets.
So the first way we can do this is say four X in hash set.
Intersection and then pass in a reference to the second set and now we can print line.
And we're going to say intersection.
X.
And so we see we have one and three in both sets and intersection is.
Has printed out one in three between the two sets.
So another way that we can do this, a more shorthand way, is we can actually say let intersection.
Equal to a reference to the first set.
The.
Binary bitwise and operator, so the ampersand sign and then a reference to the second set.
So now intersection is going to contain.
It's going to be a set that's going to contain.
The values that are in both sets.
So let's print that out for X, an intersection print line, and then we're going to say shorthand way.
And now X and we expect this to still be the same results of one and three.
Which is what we got so that this is a shorthand, way short shorthand intersection using the bitwise.
The excuse me, the binary bitwise and person operator reader.
Another one that we have is union and unions going to return values that are in either the first hash
set or the second hash set?
So I'm only going to show you the shorthand way because otherwise it's going to be the same way above.
Instead of it being hash set, then method called intersection, our method call would be union.
So let's do our shorthand way and say a reference to the hash set with the binary or.
Operator and then set.
Two.
So now union is going to be a set that's going to contain values that are that are in either of the
two hash sets.
So let's print this out.
To see to make sure that we got the correct values union.
And then X.
And for union in.
There we go.
And now if we run this, we have union.
One, two, three, four, five and seven.
So we can see that we didn't have duplicate values of one or three, but we have all the values occurring
once between the two sets.
And then lastly, we can use we can do the difference.
So likewise difference would be called like this, but we're going to do the shorthand way and say let
diff equals a reference to the first hash set minus.
So the difference between the second hash set and now we can print this out.
And see what the difference is and we'll say diff.
An X.
And so with the difference, we want to see what values are in the first hash set, but not the second
hash.
So if we run this, we see that we have two and four.
And that would be correct because we have two, four, and then the second hash.
Yet we do not have two and four.
So there are more methods we can use on a set, but these are some of the main ones.
And as you can see, we can do a lot of logic handling very quickly on sets.
Again, these are just some of the collections we have available to us in Rust, so feel free to check
out more collections.
But again, later on down the road, I do plan to show how we can create some of the data structures
available to use in rest so that we can continue to learn and become more proficient in our rest journey.