Match is going to do exactly what it sounds like it will do.
It is going to allow us to compare a value against a series of patterns and then execute code based
on the matching pattern.
So we did it earlier in one of our examples when we were setting up a pet a pet enum.
And what we did was basically said, hey, match on yourself, and if you're a dog, I want you to print
out I am a dog.
So in essence, that's what the match statement does.
So let's create another example and we'll create a new function called F and plus one.
And we're going to take in an option I 32 and we want to return an option of Type II 32.
And then in here we're going to say we want to match on X.
If none is passed in, then that's what we want to return.
None.
But if something.
This past in.
Then we want to return some.
Of I plus one.
So when we run this or call this, I should say we'll say let five equals sum I.
Right.
So this is what.
This is essentially what's going to get converted to some five right there.
That's some of I.
And then we say let six equals plus one of five.
And then we'll say Let none equals.
Plus one.
Of none.
And then let's print it out.
So we'll print it out.
And we'll have six in here.
And it's probably going to want it to be.
Just like that.
Back to the terminal.
Cargo run it.
And that's exactly what we got returned with some six.
So now if we come back in here and we do none, we would expect none to be returned.
And that's exactly what happened.
So what this function did.
Was it accepted a parameter of X which we passed in here was five, and then inside of the five variable
contained some five and we matched on some of I.
So any value could have been passed in here.
And then whatever the value that sum contained, we added one to it.
So what happens if we forget to add in the num statement?
We already see we get an air coming.
So it says we've got an air pattern.
None not covered, so the compiler will catch it for us.
Matches in rust are exhaustive, meaning that every possibility must be exhausted for the code to be
valid.
So we need to make sure that we cover every case because otherwise it will likely introduce a bug in
our code.
So we can use catch all patterns to be able to handle all other situations.
So let me come back in here and we'll just re put in none.
And then let's go back and look at our pet name and show that for all other pets, we will print out
something different.
So we want to come up here and.
Actually, we'll come down here and we'll create a function that says What pet?
And then in here we'll take our input, which is going to be a reference to a string slice.
And then we don't need to return anything in this case, but we're going to match on input.
So what we'll do in here is we'll say, Hey, we have dog and we're going to return.
I have a dog.
And then for all of our other cases, we'll have.
Are.
Fish.
We'll have our cat.
And then we can see that our input is already kind of like kitten to us, Hey, something needs to be
covered.
So if we hover over it, it'll tell us the non-exhaustive pattern is not covered and it's telling us
to use an underscore.
So if we put in an underscore here and then just say, Hey.
I have no clue what pet you have.
Well, now it seems to work because we're using this underscore to cover all other situations.
So now let's test this out real quick.
We'll say what pet and we'll say dog, what pet cat, what pet and cow.
So now if we run this, we expect to see I have a dog, I have a cat and I have no clue what pet you
have.
And that's exactly what we got.
So it's important to remember that match is exhaustive.
So we have to try to cover every single case that we could have.
And then we can use this little underscore right here.
To basically say, hey, if it doesn't match anything else, then by default run this.