So in this lecture, I want to look at how and where we can use just additional patterns and the type
of syntax is we have available to us.
So there are many different syntax and ways that we can.
Create a match arm.
But I just want to introduce you to a few, just to kind of show you what all is available to us.
So let's create a variable X and assign it to one.
And then we're going to match on X.
And then inside here we want to say one or two.
So if the value of X is one or two, then we want to print out one or two.
And then to make our match exhaustive, we want to say anything else.
Let's print out.
Not one or two.
So when we run this with X being one, we expect it to print out one or two, which is exactly what
happens.
And then just to verify that we are getting the desired behavior, change X to ten and rerun it, and
we see that we get not one or two.
So this syntax up here with this operator is the OR call the OR operator, I should say.
And what it's doing is it is saying, hey, if X is one or two.
Then.
This statement is true.
So execute the following code, which is exactly what happened.
So let's comment that out and let's look at a different way.
So now we're going to match on.
An exclusive range of values so we'll leave x is equal to ten will match on X again.
And in this case, we'll say 1 to 5.
And if it matches in that range, we'll print out matches.
Otherwise we're going to print out.
Not matching.
So we see that we've got our little red squiggly.
Let's check out what it says.
And it's telling us the exclusive range pattern is experimental.
So to fix this, we need to add in the little equal sign.
So now it's going to be 1 to 5, which includes five.
So with our X being ten, we expect this to print out, not matching.
But when we go to five, I anticipate it saying that it matches, which is exactly what happened.
So this is how we can use an inclusive range to check over a a range of values to see if something matches.
And then lastly, we can use an if an excuse me and if condition specified after the pattern and a match.
So let's check this out.
So let's say let x equals to sum five and then we'll say let y equals five, and then we're going to
match on x.
And we'll say some ten.
And then we're going to print out ten, but then we're going to do some x.
If X equals Y, then we want to print out.
Matches.
And then otherwise, we just want to print out our default statement and for ease, we'll just print
out default.
So now we have some five Y equals to five and we're going to match on X.
So when we run this, we see that it says it matches.
So how is this working?
So we're passing in X to our match statement and then X is some value and then X is in here as well,
which in this case it is going to be five.
So if five, which is referring to this x right here, equals y, which is five.
Then in this case, we know that five equals five.
So.
We are able to execute this block of code.
So now if I change this to ten.
So X is going to be some ten, then we anticipate ten being printed out.
Which is exactly what happens.
And then obviously, if I put in 15 here, so we're not going to.
This is going to be false.
This will be false.
Well, then we expect default to be printed and that's what we get.
So through the last few lectures we have learned about idioms and pattern matching, which is a very
powerful feature Rust has.
And as I mentioned earlier, we were going to cover generics in the next section.
So let's continue on with what traits and generics are.