Enumerations or e nums for short allow you to define a type by enumerating its possible variants.
So let's start by looking at how to define an enum.
So it's relatively the same as a shrug.
So we'll say enum and then we'll create an enumeration of pets.
So we'll call it Pet, and then we'll open and close it with curly brackets.
And then in here we'll have a dog, a cat and a fish.
So to use this, then we can just say let dog equals pet of dog.
So just like structs, we can also implement methods on them.
So let's pull pet open and close and then let's say let's create a function called What Am I?
And remember, you have to reference yourself.
And then we're going to return.
String slice.
And then inside here, we're just going to say,
hey, match and we'll get into this in a second, self.
Pet.
Dog.
Neither capitalize the p pet dog and if it matches, return.
I am a dog.
And then we're going to do this again for.
Cat and fish.
So there is something that I purposefully left out.
So let's see what it is.
It says we are expecting a string, but we return to string slice.
So now let's return a reference to our string slice.
But now when we do that, we need to specify our lifetime annotation and we want it to be a static lifetime.
So now we're all good.
So down here, let's, let's print this out just to make sure everything worked.
So we're going to have a dog and we're going to say, What am I?
So we're expecting it to say, I am a dog.
And that's exactly what it says.
It says, I am a doc.
So awesome.
It worked.
So we can also do that for cat and fish and they would return.
I am a cat.
I am a fish.
But now let's look at how we can use an enum inside of a struct.
So we're going to shift gears for a second.
We're going to create another enum called IP addr kind.
So the IP address kind and we're going to have IP four and then also six.
So now inside of here, we're going to create a struct for IP address and for our kind, we're going
to assign it an IP address because our IP can either be IP V four or IPV six and then our address is
obviously going to be a string.
So down here, let's say let home equal I.
Peter.
And it's going the kind.
Is going to be iPad or kind v four.
And then our address is going to be a string.
From.
120 7001.
So local host.
And close it so we can also do let loopback
IP adder.
Kind, IP matter kind.
And in this case, it's going to be version six.
And then our address is going to be that.
So that's how we can use an E numb inside of a stroke.
We can also have structs inside of enemies.
And then furthermore, we can assign data types inside our enum as well.
So let's say we have this up here, so we're going to assign a data type to it, and we want this to
be a string.
So down here we can modify, we'll say let home.
Equals I.
Peter.
V four and then we need it to be a string.
So then we have string from 120 7001.
So we have a little air.
Let's see what it says.
No associated name.
And that's because this needs to say kind.
So that's how we can assign a type inside of our enum so you can get creative as you want.
With types inside of an enum, you can have variants with no data associated.
Named fields like a stroke.
You can specify a string or specify as many types inside, just like you can with a tuple.
So the options are quite extensive, which is great.
So now let's look at some enemies that rust gives us by default.