>> In this video, I'll explore
if/else and match control flow operators
in more detail with demos.
I'll begin by creating a new project called Control Flow.
I'll do that in my terminal window now.
Once I have my rust projects directory and the terminal window,
I will type cargo new control_flow.
Then once that package is created,
I'll go ahead and change directories to
the control flow package and open that up in Visual Studio Code.
Once the project is open in Visual Studio Code,
I'll focus my attention on the main.rs program.
Inside of the main function,
I'll go ahead and get rid of this print statement.
Let's go ahead and start by writing
a couple simple if/else expressions.
The first I will say if one is equal to two,
then I want to print math is broken.
Else, I can go ahead and say print everything is fine.
Cool. In this example,
the condition of if is the expression one is equal to two,
which evaluates into a Boolean type with the value false.
Unlike in most languages,
if blocks can also act as expressions.
Remember that all branches must return
the same type for our code to compile.
Let's look at another example of that now.
I'll define a variable called formal that is equal to true.
Then I want to define another variable called greeting,
which is going to be equal to an if expression called if formal.
If formal is equal to true,
I want to go ahead and print out good evening.
Else, if formal is equal to false,
I want to go ahead and print out hey,
friend and add a semicolon at the end of that and
also a semicolon at the end of this expression here.
In this example, we can see that the result of the
if expression is stored in the greeting variable.
Then from that, we're able to use
this new if expression to
evaluate what to print out as the greeting.
You can handle multiple conditions by combining
if and else and an else if expression.
For example, let's say let number is equal to six
and then if number modulo four is equal to zero.
I want to go ahead and print out number is divisible by four.
Else if number modulo three is equal to zero.
Then I want to say print out n number is divisible by three.
Finally, I can say else println number is
not divisible by four or three and add
a semicolon at the end of that.
I can also fix my typo appeared thanks for this code for calling
that out for me is not divisible by three.
Here is just a simple example where we can have
multiple branches and you can add as many branches as you want.
If you go ahead and add in that out if keyword.
Let's go ahead and look at
one more example where we can use match.
To begin in this example,
I want to define a variable called Boolean,
which is going to be equal to true.
Then I want to say let binary is
equal to match Boolean and then add some braces.
Now I'm going to print out all of the possible values,
which for a Boolean is really straightforward.
I'm going to have either zero or I'm going to have one.
Then there we have it.
I can add a semicolon at the end of that.
Now we define Boolean is equal to true.
When it comes down to evaluate the match statement,
it's going to go through these two different ORMs here.
It's going to skip the first ORM since that is
equal to false and then select
true as the matching ORM so that
the value printed out will be one.
There's a few examples of using if/else and match in action.
Continue to explore on your own
to get more familiar with these expressions.
[MUSIC]