- So in this section we're going to look
at how to write if statements in Rust.
Obviously, an important point.
Nothing particularly difficult,
but there are a few syntax surprises
compared to other languages, okay?
So it's worth discussing.
So here's an example of the basic syntax
for an if statement.
So I've declared available, my age is 58.
That is my age actually.
And the if statement checks,
if the person age is greater than 50,
then it prints out you are old.
A little bit rude.
So some points to note here, compared to the syntax
of other languages, the test expression must be a boolean.
Okay, so that's unlike, for example, in C++ you could say...
In C++ you could say something like, if x, okay?
And what you're really saying is if x is non-zero.
So that wouldn't work in Rust.
In Rust, you'd have to explicitly have a boolean result.
If x is not equal to zero, okay?
You have to have a boolean expression
for your test condition, okay?
So that's one point.
No need for parentheses around the test.
So you might be tempted to put parentheses here, okay?
And it would compile, but you get a warning.
Rust, the compiler, is quite fussy.
If you put unnecessary parentheses around the test
then you'll get a warning from the compiler, okay?
So I guess I should leave them out in that case like so.
And one other point,
which would be a surprise for most developers
is that you must always use curly brackets
to contain the body of your if statement.
Even if there's only one statement in the if block
you still have to use curly brackets
for the beginning and end, okay?
And that's different for most other languages as well,
like in C and C++ and Java and C#.
If you just had a single statement
then you would quite cheerfully leave off the curly brackets
or you could do anyway.
As a developer in those other languages
I would've put them in anyway
just in case I then add another statement
later on into my body.
But in Rust you have to.
You must have curly brackets,
even if there's only one statement in the block, okay?
So that's a point to note.
You can also have if-else tests.
Obviously, so here, another bit of information about myself.
I am 1.6 meters, 1.67 meters tall.
And it says, if your height is greater than 1.8,
you are tall.
Otherwise you are not so tall, okay?
So there's no real surprises here.
Again, the expression must be a boolean.
You must use code brackets around each part.
So this is the beginning and end of the true part.
This is the beginning and end of the false part.
You have to put those color brackets in.
So that's an if-else test.
And you can have a multi-way if-else test as well.
So a little bit of a insight here.
So first of all, Rust likes your variable names
to use snake case, lowercase with underscores.
So I've got a variable called swans_game.
So the Swans in my local football team, Swansea City,
in case you didn't know that from before.
And I've been following Swansea since 1978 when I was 14.
And I don't know how many games I've seen
but it's probably too many.
Not a lot of joy unfortunately
but that's how it is being a football fan.
So I've been to see 500 games
and I've got an if-else-if test, a multi-way if statement.
And if you're wondering, there is also something similar
to a switch statement in Rust.
There's a keyword called match, which is like switch.
We'll see that in the next section.
But in here, if you've seen more than 300 games
then you are a loyal fan.
If you haven't seen more than 300
then it goes into the else-if branch.
Maybe you've seen more than 100,
in which case you are a discerning fan.
You choose which games to watch and which ones not to watch.
If you come into the ELs close at the bottom
then it must mean that you've seen 100 or fewer.
So that means you're quite a new fan
or maybe not a fan at all.
That's a shock. So there aren't many surprises there.
I think once we've got over the fact
that you've gotta have a boolean test,
you've gotta have the curly brackets,
no parentheses around the test conditions,
then this is pretty much what you might expect to see.
Okay, right, one other thing.
You can also have an if-else as an expression.
In the middle of another statement
you can have an if-else expression, like I've shown here.
So what you do is you say if, and then you have a test.
If the test is true, then this will be the result
of the expression, which is then assigned to the variable.
If you're older, if your age is greater than 50
then you are old.
A bit rude again.
If the test fails,
then it picks up the else condition here, okay?
So either that condition or this condition
will be the result of the whole thing.
This whole expression will evaluate
to be either the true outcome that gets assigned
or the false outcome that gets assigned like so, okay?
So that's similar.
If you've seen other languages like Java or C#,
or pretty much any other language,
then that would be similar to the turn of the operator,
the question mark colon operator,
or the conditional operator I think you might call it
in other languages.
This is the equivalent in Rust.
Okay, so example time.
The demo project for this chapter is lesson03_flow_control.
Again, this is a cargo project.
I created lesson03_flow_control using cargo_new.
And in that project I've got various functions,
one function per section in the chapter
just to give us a bit of focus.
Let's have a look.
So first of all, here's my rustdev folder
on my file system.
That's where we are, lesson03_flow_control.
So if you open that folder in code editor, here we are.
This is lesson03_flow_control.
My main_rs has the main function
and I've got four demos, four demo functions.
We'll have a look at demo_if.
Let's uncomment that first of all.
So demo_if, and let's take a look.
So basically the same code as per the slides.
So I'll run it in the terminal
so I can kind of discuss the output
while I'm actually discussing the code at the same time.
cargo run.
I'm also gonna try some changes here as well
to see what kind of warnings I get
if I'm not too careful with my parentheses and such.
So at the top, I am 58. A shock to me.
Last time I looked I was 21.
Here's my if statement. Are you old?
And it says, yes, you are. You are old.
Thank you very much. Here's my height.
What does it say about my height?
It says you are not so tall. Okay.
And then how about my multi-branch if statement?
The number of games I've seen with the Swans is 500.
I'm gonna come into the very loyal fan category.
We appreciate it, dude. Well, there we go.
You are a very loyal fan, we appreciate it, dude.
Okay, so that's my kind of multi-branch if statement.
And then finally,
let's see is my age either going to be old?
Yes, it is, isn't it? I'm officially old.
You are old. You're old.
I think it should say, you are old, I guess.
Anyway, you get the message.
What would happen
if I accidentally put parentheses around my test?
Let's see what would happen there.
Okay, so looking at my code, so I've got a warning
about unnecessary parentheses around the if condition.
You would think that Rust wouldn't bother complaining
about such minutia, but it does.
It's trying to make your code as consistent as possible.
There is a warning, a compiler flag basically,
which is enabled here by default
that unused parentheses will give me a warning.
It suggests rewriting my code that's currently like that
into this without the parentheses, okay?
You're never gonna win against Rust
so you may as well just fall in and follow the rules.
Okay, what about if I forget the parentheses? I wonder.
Not the parentheses, the curly brackets.
I've deleted the curly brackets from here and here.
I wonder what's gonna happen now when I re-compile the code.
Oop, now it's an error.
It really does require a curly bracket kind of here, okay?
You could put it here if you wanted to, but that would be...
That's not really the layer that Rust expects.
So let's do it the way Rust expects.
I have to put curly brackets in
even though it's only a single statement like so.
Okay, and one last thing, what if I had a test
which was implicitly test against zero?
So let's say, let x equals zero,
and I'm gonna write the test, if x then println.
What I'm trying to do is I'm trying to see if x is non-zero.
x is non-zero, dude.
Okay. Like so.
So I'm expecting a compiler error here
because this is an integer, implicitly an integer,
and that's not a boolean test.
I'm expecting an error
to say that the test condition must be a boolean expression.
Let's see.
Yes, okay, so on line 15 here...
Excusez-moi, on line 15,
it was expecting this to be a boolean
and it found an integer.
So I have to, if I want to test for non-zero
I have to explicitly test for non-zero like that, okay?
So again, that's a word of caution for C++ developers.
Okay, so that's the basic if statement.
That's the end of this section.
What we'll do in the next section
is have a look at the match keyword,
which is kind of like switch in other languages.