1 00:00:06,570 --> 00:00:08,550 - So in this section we're going to look 2 00:00:08,550 --> 00:00:10,470 at how to write if statements in Rust. 3 00:00:10,470 --> 00:00:11,970 Obviously, an important point. 4 00:00:12,840 --> 00:00:14,460 Nothing particularly difficult, 5 00:00:14,460 --> 00:00:16,380 but there are a few syntax surprises 6 00:00:16,380 --> 00:00:18,120 compared to other languages, okay? 7 00:00:18,120 --> 00:00:19,980 So it's worth discussing. 8 00:00:19,980 --> 00:00:21,960 So here's an example of the basic syntax 9 00:00:21,960 --> 00:00:23,670 for an if statement. 10 00:00:23,670 --> 00:00:26,790 So I've declared available, my age is 58. 11 00:00:26,790 --> 00:00:28,500 That is my age actually. 12 00:00:28,500 --> 00:00:30,660 And the if statement checks, 13 00:00:30,660 --> 00:00:33,540 if the person age is greater than 50, 14 00:00:33,540 --> 00:00:35,913 then it prints out you are old. 15 00:00:36,750 --> 00:00:38,250 A little bit rude. 16 00:00:38,250 --> 00:00:41,100 So some points to note here, compared to the syntax 17 00:00:41,100 --> 00:00:46,100 of other languages, the test expression must be a boolean. 18 00:00:46,740 --> 00:00:51,540 Okay, so that's unlike, for example, in C++ you could say... 19 00:00:51,540 --> 00:00:55,013 In C++ you could say something like, if x, okay? 20 00:00:55,929 --> 00:00:59,880 And what you're really saying is if x is non-zero. 21 00:00:59,880 --> 00:01:02,070 So that wouldn't work in Rust. 22 00:01:02,070 --> 00:01:05,130 In Rust, you'd have to explicitly have a boolean result. 23 00:01:05,130 --> 00:01:08,370 If x is not equal to zero, okay? 24 00:01:08,370 --> 00:01:11,460 You have to have a boolean expression 25 00:01:11,460 --> 00:01:13,800 for your test condition, okay? 26 00:01:13,800 --> 00:01:14,823 So that's one point. 27 00:01:15,660 --> 00:01:18,900 No need for parentheses around the test. 28 00:01:18,900 --> 00:01:22,973 So you might be tempted to put parentheses here, okay? 29 00:01:23,970 --> 00:01:26,820 And it would compile, but you get a warning. 30 00:01:26,820 --> 00:01:29,370 Rust, the compiler, is quite fussy. 31 00:01:29,370 --> 00:01:32,640 If you put unnecessary parentheses around the test 32 00:01:32,640 --> 00:01:36,390 then you'll get a warning from the compiler, okay? 33 00:01:36,390 --> 00:01:41,070 So I guess I should leave them out in that case like so. 34 00:01:41,070 --> 00:01:42,810 And one other point, 35 00:01:42,810 --> 00:01:45,840 which would be a surprise for most developers 36 00:01:45,840 --> 00:01:48,660 is that you must always use curly brackets 37 00:01:48,660 --> 00:01:53,580 to contain the body of your if statement. 38 00:01:53,580 --> 00:01:57,360 Even if there's only one statement in the if block 39 00:01:57,360 --> 00:01:59,370 you still have to use curly brackets 40 00:01:59,370 --> 00:02:01,440 for the beginning and end, okay? 41 00:02:01,440 --> 00:02:03,810 And that's different for most other languages as well, 42 00:02:03,810 --> 00:02:06,447 like in C and C++ and Java and C#. 43 00:02:08,040 --> 00:02:09,870 If you just had a single statement 44 00:02:09,870 --> 00:02:14,460 then you would quite cheerfully leave off the curly brackets 45 00:02:14,460 --> 00:02:16,650 or you could do anyway. 46 00:02:16,650 --> 00:02:18,810 As a developer in those other languages 47 00:02:18,810 --> 00:02:20,970 I would've put them in anyway 48 00:02:20,970 --> 00:02:23,190 just in case I then add another statement 49 00:02:23,190 --> 00:02:24,690 later on into my body. 50 00:02:24,690 --> 00:02:26,520 But in Rust you have to. 51 00:02:26,520 --> 00:02:27,840 You must have curly brackets, 52 00:02:27,840 --> 00:02:31,700 even if there's only one statement in the block, okay? 53 00:02:31,700 --> 00:02:33,303 So that's a point to note. 54 00:02:34,140 --> 00:02:36,510 You can also have if-else tests. 55 00:02:36,510 --> 00:02:39,330 Obviously, so here, another bit of information about myself. 56 00:02:39,330 --> 00:02:43,380 I am 1.6 meters, 1.67 meters tall. 57 00:02:43,380 --> 00:02:46,620 And it says, if your height is greater than 1.8, 58 00:02:46,620 --> 00:02:48,030 you are tall. 59 00:02:48,030 --> 00:02:50,610 Otherwise you are not so tall, okay? 60 00:02:50,610 --> 00:02:52,653 So there's no real surprises here. 61 00:02:53,670 --> 00:02:56,940 Again, the expression must be a boolean. 62 00:02:56,940 --> 00:02:59,400 You must use code brackets around each part. 63 00:02:59,400 --> 00:03:03,180 So this is the beginning and end of the true part. 64 00:03:03,180 --> 00:03:05,550 This is the beginning and end of the false part. 65 00:03:05,550 --> 00:03:07,550 You have to put those color brackets in. 66 00:03:09,270 --> 00:03:11,433 So that's an if-else test. 67 00:03:12,420 --> 00:03:15,780 And you can have a multi-way if-else test as well. 68 00:03:15,780 --> 00:03:18,060 So a little bit of a insight here. 69 00:03:18,060 --> 00:03:20,670 So first of all, Rust likes your variable names 70 00:03:20,670 --> 00:03:24,060 to use snake case, lowercase with underscores. 71 00:03:24,060 --> 00:03:26,180 So I've got a variable called swans_game. 72 00:03:26,180 --> 00:03:28,983 So the Swans in my local football team, Swansea City, 73 00:03:29,970 --> 00:03:32,130 in case you didn't know that from before. 74 00:03:32,130 --> 00:03:37,130 And I've been following Swansea since 1978 when I was 14. 75 00:03:39,060 --> 00:03:41,820 And I don't know how many games I've seen 76 00:03:41,820 --> 00:03:44,610 but it's probably too many. 77 00:03:44,610 --> 00:03:46,740 Not a lot of joy unfortunately 78 00:03:46,740 --> 00:03:49,413 but that's how it is being a football fan. 79 00:03:50,550 --> 00:03:53,070 So I've been to see 500 games 80 00:03:53,070 --> 00:03:57,420 and I've got an if-else-if test, a multi-way if statement. 81 00:03:57,420 --> 00:04:00,690 And if you're wondering, there is also something similar 82 00:04:00,690 --> 00:04:02,880 to a switch statement in Rust. 83 00:04:02,880 --> 00:04:05,580 There's a keyword called match, which is like switch. 84 00:04:05,580 --> 00:04:07,860 We'll see that in the next section. 85 00:04:07,860 --> 00:04:11,940 But in here, if you've seen more than 300 games 86 00:04:11,940 --> 00:04:13,890 then you are a loyal fan. 87 00:04:13,890 --> 00:04:15,870 If you haven't seen more than 300 88 00:04:15,870 --> 00:04:17,730 then it goes into the else-if branch. 89 00:04:17,730 --> 00:04:19,170 Maybe you've seen more than 100, 90 00:04:19,170 --> 00:04:21,960 in which case you are a discerning fan. 91 00:04:21,960 --> 00:04:25,500 You choose which games to watch and which ones not to watch. 92 00:04:25,500 --> 00:04:28,050 If you come into the ELs close at the bottom 93 00:04:28,050 --> 00:04:31,200 then it must mean that you've seen 100 or fewer. 94 00:04:31,200 --> 00:04:33,090 So that means you're quite a new fan 95 00:04:33,090 --> 00:04:35,730 or maybe not a fan at all. 96 00:04:35,730 --> 00:04:39,030 That's a shock. So there aren't many surprises there. 97 00:04:39,030 --> 00:04:40,590 I think once we've got over the fact 98 00:04:40,590 --> 00:04:42,690 that you've gotta have a boolean test, 99 00:04:42,690 --> 00:04:44,460 you've gotta have the curly brackets, 100 00:04:44,460 --> 00:04:47,340 no parentheses around the test conditions, 101 00:04:47,340 --> 00:04:50,223 then this is pretty much what you might expect to see. 102 00:04:51,480 --> 00:04:53,550 Okay, right, one other thing. 103 00:04:53,550 --> 00:04:57,090 You can also have an if-else as an expression. 104 00:04:57,090 --> 00:04:59,580 In the middle of another statement 105 00:04:59,580 --> 00:05:03,480 you can have an if-else expression, like I've shown here. 106 00:05:03,480 --> 00:05:07,620 So what you do is you say if, and then you have a test. 107 00:05:07,620 --> 00:05:11,430 If the test is true, then this will be the result 108 00:05:11,430 --> 00:05:15,240 of the expression, which is then assigned to the variable. 109 00:05:15,240 --> 00:05:18,030 If you're older, if your age is greater than 50 110 00:05:18,030 --> 00:05:19,710 then you are old. 111 00:05:19,710 --> 00:05:21,780 A bit rude again. 112 00:05:21,780 --> 00:05:24,210 If the test fails, 113 00:05:24,210 --> 00:05:26,580 then it picks up the else condition here, okay? 114 00:05:26,580 --> 00:05:29,640 So either that condition or this condition 115 00:05:29,640 --> 00:05:31,740 will be the result of the whole thing. 116 00:05:31,740 --> 00:05:34,710 This whole expression will evaluate 117 00:05:34,710 --> 00:05:38,310 to be either the true outcome that gets assigned 118 00:05:38,310 --> 00:05:43,310 or the false outcome that gets assigned like so, okay? 119 00:05:43,950 --> 00:05:45,540 So that's similar. 120 00:05:45,540 --> 00:05:49,350 If you've seen other languages like Java or C#, 121 00:05:49,350 --> 00:05:51,510 or pretty much any other language, 122 00:05:51,510 --> 00:05:55,710 then that would be similar to the turn of the operator, 123 00:05:55,710 --> 00:05:57,690 the question mark colon operator, 124 00:05:57,690 --> 00:05:59,880 or the conditional operator I think you might call it 125 00:05:59,880 --> 00:06:00,960 in other languages. 126 00:06:00,960 --> 00:06:03,930 This is the equivalent in Rust. 127 00:06:03,930 --> 00:06:06,273 Okay, so example time. 128 00:06:07,140 --> 00:06:12,140 The demo project for this chapter is lesson03_flow_control. 129 00:06:12,270 --> 00:06:14,610 Again, this is a cargo project. 130 00:06:14,610 --> 00:06:18,930 I created lesson03_flow_control using cargo_new. 131 00:06:18,930 --> 00:06:21,000 And in that project I've got various functions, 132 00:06:21,000 --> 00:06:23,640 one function per section in the chapter 133 00:06:23,640 --> 00:06:26,550 just to give us a bit of focus. 134 00:06:26,550 --> 00:06:28,020 Let's have a look. 135 00:06:28,020 --> 00:06:30,240 So first of all, here's my rustdev folder 136 00:06:30,240 --> 00:06:31,410 on my file system. 137 00:06:31,410 --> 00:06:34,320 That's where we are, lesson03_flow_control. 138 00:06:34,320 --> 00:06:38,880 So if you open that folder in code editor, here we are. 139 00:06:38,880 --> 00:06:41,160 This is lesson03_flow_control. 140 00:06:41,160 --> 00:06:43,890 My main_rs has the main function 141 00:06:43,890 --> 00:06:46,620 and I've got four demos, four demo functions. 142 00:06:46,620 --> 00:06:48,270 We'll have a look at demo_if. 143 00:06:48,270 --> 00:06:50,460 Let's uncomment that first of all. 144 00:06:50,460 --> 00:06:54,603 So demo_if, and let's take a look. 145 00:06:55,860 --> 00:06:58,560 So basically the same code as per the slides. 146 00:06:58,560 --> 00:07:00,210 So I'll run it in the terminal 147 00:07:00,210 --> 00:07:02,040 so I can kind of discuss the output 148 00:07:02,040 --> 00:07:04,823 while I'm actually discussing the code at the same time. 149 00:07:05,700 --> 00:07:06,813 cargo run. 150 00:07:08,280 --> 00:07:10,290 I'm also gonna try some changes here as well 151 00:07:10,290 --> 00:07:11,820 to see what kind of warnings I get 152 00:07:11,820 --> 00:07:15,543 if I'm not too careful with my parentheses and such. 153 00:07:16,440 --> 00:07:20,100 So at the top, I am 58. A shock to me. 154 00:07:20,100 --> 00:07:22,110 Last time I looked I was 21. 155 00:07:22,110 --> 00:07:24,660 Here's my if statement. Are you old? 156 00:07:24,660 --> 00:07:26,820 And it says, yes, you are. You are old. 157 00:07:26,820 --> 00:07:30,513 Thank you very much. Here's my height. 158 00:07:31,530 --> 00:07:34,290 What does it say about my height? 159 00:07:34,290 --> 00:07:38,340 It says you are not so tall. Okay. 160 00:07:38,340 --> 00:07:41,370 And then how about my multi-branch if statement? 161 00:07:41,370 --> 00:07:44,190 The number of games I've seen with the Swans is 500. 162 00:07:44,190 --> 00:07:46,920 I'm gonna come into the very loyal fan category. 163 00:07:46,920 --> 00:07:50,010 We appreciate it, dude. Well, there we go. 164 00:07:50,010 --> 00:07:53,823 You are a very loyal fan, we appreciate it, dude. 165 00:07:55,350 --> 00:07:59,220 Okay, so that's my kind of multi-branch if statement. 166 00:07:59,220 --> 00:08:00,693 And then finally, 167 00:08:01,710 --> 00:08:04,680 let's see is my age either going to be old? 168 00:08:04,680 --> 00:08:07,110 Yes, it is, isn't it? I'm officially old. 169 00:08:07,110 --> 00:08:09,753 You are old. You're old. 170 00:08:10,800 --> 00:08:13,170 I think it should say, you are old, I guess. 171 00:08:13,170 --> 00:08:14,400 Anyway, you get the message. 172 00:08:14,400 --> 00:08:15,310 What would happen 173 00:08:16,200 --> 00:08:20,733 if I accidentally put parentheses around my test? 174 00:08:22,020 --> 00:08:24,543 Let's see what would happen there. 175 00:08:27,182 --> 00:08:32,182 Okay, so looking at my code, so I've got a warning 176 00:08:34,860 --> 00:08:39,540 about unnecessary parentheses around the if condition. 177 00:08:39,540 --> 00:08:42,300 You would think that Rust wouldn't bother complaining 178 00:08:42,300 --> 00:08:44,343 about such minutia, but it does. 179 00:08:45,240 --> 00:08:48,693 It's trying to make your code as consistent as possible. 180 00:08:49,680 --> 00:08:53,103 There is a warning, a compiler flag basically, 181 00:08:53,970 --> 00:08:56,490 which is enabled here by default 182 00:08:56,490 --> 00:08:59,733 that unused parentheses will give me a warning. 183 00:09:01,080 --> 00:09:04,590 It suggests rewriting my code that's currently like that 184 00:09:04,590 --> 00:09:08,280 into this without the parentheses, okay? 185 00:09:08,280 --> 00:09:10,590 You're never gonna win against Rust 186 00:09:10,590 --> 00:09:13,293 so you may as well just fall in and follow the rules. 187 00:09:14,400 --> 00:09:18,123 Okay, what about if I forget the parentheses? I wonder. 188 00:09:19,260 --> 00:09:21,840 Not the parentheses, the curly brackets. 189 00:09:21,840 --> 00:09:25,800 I've deleted the curly brackets from here and here. 190 00:09:25,800 --> 00:09:29,400 I wonder what's gonna happen now when I re-compile the code. 191 00:09:29,400 --> 00:09:30,903 Oop, now it's an error. 192 00:09:32,744 --> 00:09:37,744 It really does require a curly bracket kind of here, okay? 193 00:09:39,390 --> 00:09:43,830 You could put it here if you wanted to, but that would be... 194 00:09:43,830 --> 00:09:46,860 That's not really the layer that Rust expects. 195 00:09:46,860 --> 00:09:49,440 So let's do it the way Rust expects. 196 00:09:49,440 --> 00:09:51,240 I have to put curly brackets in 197 00:09:51,240 --> 00:09:54,003 even though it's only a single statement like so. 198 00:09:55,587 --> 00:10:00,587 Okay, and one last thing, what if I had a test 199 00:10:00,750 --> 00:10:03,570 which was implicitly test against zero? 200 00:10:03,570 --> 00:10:07,650 So let's say, let x equals zero, 201 00:10:07,650 --> 00:10:12,570 and I'm gonna write the test, if x then println. 202 00:10:12,570 --> 00:10:16,650 What I'm trying to do is I'm trying to see if x is non-zero. 203 00:10:16,650 --> 00:10:20,793 x is non-zero, dude. 204 00:10:22,080 --> 00:10:24,453 Okay. Like so. 205 00:10:26,790 --> 00:10:29,550 So I'm expecting a compiler error here 206 00:10:29,550 --> 00:10:32,970 because this is an integer, implicitly an integer, 207 00:10:32,970 --> 00:10:35,880 and that's not a boolean test. 208 00:10:35,880 --> 00:10:37,680 I'm expecting an error 209 00:10:37,680 --> 00:10:41,010 to say that the test condition must be a boolean expression. 210 00:10:41,010 --> 00:10:41,843 Let's see. 211 00:10:43,560 --> 00:10:47,597 Yes, okay, so on line 15 here... 212 00:10:49,685 --> 00:10:51,480 Excusez-moi, on line 15, 213 00:10:51,480 --> 00:10:53,520 it was expecting this to be a boolean 214 00:10:53,520 --> 00:10:55,620 and it found an integer. 215 00:10:55,620 --> 00:10:58,530 So I have to, if I want to test for non-zero 216 00:10:58,530 --> 00:11:03,090 I have to explicitly test for non-zero like that, okay? 217 00:11:03,090 --> 00:11:07,440 So again, that's a word of caution for C++ developers. 218 00:11:07,440 --> 00:11:09,300 Okay, so that's the basic if statement. 219 00:11:09,300 --> 00:11:11,160 That's the end of this section. 220 00:11:11,160 --> 00:11:12,300 What we'll do in the next section 221 00:11:12,300 --> 00:11:14,160 is have a look at the match keyword, 222 00:11:14,160 --> 00:11:17,163 which is kind of like switch in other languages.