[No Audio]
Welcome again. This tutorial is about the
Match Statement. It is a control flow
operator in Rust, that is used to transfer
control to a particular block of code, based
on the value of the variable being tested.
Rust implements this construct using the
match keyword. It has many similarities with
the switch that is being used in other
programming languages. Let's start learning
the Match Statement.
We will start with the general syntax first.
[No Audio]
Please note that the special syntax for the
default case, where we just put an underscore
and then equal to sign.
This is a typical
value of some variable that we want to match.
Each of the possible values along with its
respective statements to execute are called
the arms of the match. In the above syntax,
we have this as one arm, this is the second
arm, and so on. When none of these arms
matches, then the default case is going to be
executed. One of the key property or
characteristic of the match is that, it is
exhaustive and covers all possible cases. And
the second property is that, it always
required that one of the arms should execute.
These two properties will become clear to
us in the examples. Also note that there are
commas at the end of each arm. Let us start
doing some examples. I will declare a
variable some_number = 100.
[No Audio]
Next I will do a match on this variable.
[No Audio]
We will next add the arms for the match, the
left side of the arms are the conditions or
values, in this case, that we want to match, and
the right side, our respective code that will
be executed when the condition is being
matched. I will match on the single value in
this arm, and will add a suitable print statement.
[No Audio]
Next I will add another arm, where I will
match on the two values. If the value happens
to have a value of two or three, then I want
this arm to be executed.
[No Audio]
A single pipe represents a logical OR in this case.
Next, we can mention a range of values
also. Range of values are being indicated by
two dots, with a starting number and ending number.
I would like to match on numbers
from 4 to 100 inclusive.
[No Audio]
Finally, if none of the arm is being matched,
then I would like to have a default arm.
The default arm is included by having an
underscore in the left side of the arm.
I will also include a suitable print
statement in this case.
[No Audio]
Now let us Cargo run this.
[No Audio]
You may note that, the value of the variable
was 100, and it has matched the third arm, and
therefore the respective print statement has
been executed. Now let us explain a few points.
If I check for the value of 2 in
the first arm, by including the relevant condition,
[No Audio]
the compiler will show a message
beneath the 2, in the second arm, telling me
that it is an unreachable pattern.
This is because in case of 2, the
first time will execute first, and therefore
the second arm will never get a chance.
And though the program will execute, but the
compiler has rightfully warned us of the
possible issue. Let me remove it for now.
Next, if we remove the default case and save,
you may note that, the compiler will complain
saying that, we have non-exhaustive patterns.
The match requires all possible cases to be
mentioned, so that at least one of the arm is
always executed. If for some reason we
mentioned all possible cases, then we may skip
the default arm for the match. An example of
all possible cases will be, when we do a
match on a boolean variable, which takes only
two values. So therefore, we can have one arm
corresponding to true value, and another arm
corresponding to the false value, and
therefore the default arm will not be needed.
[No Audio]
Let us look at some more examples. Okay, let
us look at the student grade assignment
program, that we covered in the previous tutorial,
but this time using the match statement.
I will start by declaring a
variable of marks, containing the marks of the student.
[No Audio]
I will now assign grades by doing a
match on the marks. For storing the grades,
I will declare another variable
[No Audio]
I will do a match on the marks
[No Audio]
Next, we will insert different arms
corresponding to different grades.
[No Audio]
This program will assign the appropriate
grade, based on the arm which is being matched.
Please note that we match ranges of values,
let us execute this.
[No Audio]
Please note again, that in contrast to, if-else-if,
ladder, where the else part is optional,
the requirement in the match is a bit different.
And in majority of the cases, we may
not skip the default option, and this is
because of the match needs to be exhaustive,
and needs to cover all possible cases.
Additionally also note that, in case when we
have one statement, so we may skip adding
curly brackets to the statement after the arm.
When we have multiple statements
corresponding to a certain arm, then we will
add curly brackets.
Like the if let syntax, we
can also have a let with the match
statement, let us learn this. Let us first
look at the general syntax.
[No Audio]
In this case now, the variable will be
assigned a value, based on the matching arm result.
Let us learn this with an example.
Consider the same program that we just
considered above, I will copy and paste it.
[No Audio]
Now, instead of assigning values to the
variable of grade inside the individual arms,
I will write let grade =,
at start of the match, and will delete the
assignment to the grade variables inside the arms.
[No Audio]
This is now a more compact and more refined
version of the same code, let us execute.
[No Audio]
We may note, that the program executes the
same way as in the previous case. The last
point to note is that, sometimes you may have
multiple statements inside a certain arm. In
that case, if we are using the let syntax
with the match, then the code associated with
the arm should have at least one statement,
which do not have a semicolon after it.
For instance, for the first time, I will include
some additional statement.
[No Audio]
If this arm gets executed, then the value of A
will be assigned, because this is
the statement which do not have a semicolon afterward.
Another requirement with the let
syntax is that, the returning value from the
arms must have the same type. This
requirement is the same in the if let syntax also.
This is because the Rust, needs to
know the type of the variables at compile
time. And if it is unable to reduce the type,
it will typically complain. For instance, if
inside the first time, I return a numeric
value instead of a char.
[No Audio]
Then you may note that,
the compiler will show it or saying that,
match arms have incompatible types.
That is it for this particular tutorial. We looked at
the details of the match, which share some
similarities with the if statement. However,
it has some essential differences at the same time.
In the upcoming tutorial, we will be
looking at the looping constructs.
So do come back for learning those,
and until next tutorial, happy Rust programming.
[No Audio]