1 00:00:06,570 --> 00:00:07,740 - So far in this lesson, 2 00:00:07,740 --> 00:00:09,600 we've seen how to write if statements 3 00:00:09,600 --> 00:00:11,880 and we've seen how to use the match keyword 4 00:00:11,880 --> 00:00:13,650 to do decision making. 5 00:00:13,650 --> 00:00:15,270 And what we're going to do in this section 6 00:00:15,270 --> 00:00:17,760 is to see how to write loops. 7 00:00:17,760 --> 00:00:21,300 So Rust has several different ways to write a loop. 8 00:00:21,300 --> 00:00:24,690 The first keyword we're going to look at is loop. 9 00:00:24,690 --> 00:00:26,280 As you can see here, 10 00:00:26,280 --> 00:00:31,280 it's an infinite loop basically, so it'll loop forever. 11 00:00:32,070 --> 00:00:35,100 That might be useful in a real-time system 12 00:00:35,100 --> 00:00:39,030 where you wanna have like an event-driven application. 13 00:00:39,030 --> 00:00:42,720 You have a loop, which is continuously iterating, 14 00:00:42,720 --> 00:00:46,710 waiting for an event to arrive on some kind of queue, 15 00:00:46,710 --> 00:00:49,440 dispatch the event, and then loop back again, 16 00:00:49,440 --> 00:00:52,950 ready to get the next item from the event queue. 17 00:00:52,950 --> 00:00:57,210 So this is an infinite loop in Rust. 18 00:00:57,210 --> 00:00:59,160 If you were a C++ 19 00:00:59,160 --> 00:01:02,400 or a C# or Java kind of developer, 20 00:01:02,400 --> 00:01:05,130 then you could have achieved the same effect 21 00:01:05,130 --> 00:01:06,730 right in the for loop like this. 22 00:01:09,390 --> 00:01:10,293 Like so. 23 00:01:12,061 --> 00:01:14,880 That doesn't work in Rust. 24 00:01:14,880 --> 00:01:17,520 You have to do it the way I've shown you here. 25 00:01:17,520 --> 00:01:19,320 So that's an infinite loop. 26 00:01:19,320 --> 00:01:22,890 And then next up there's a while loop. 27 00:01:22,890 --> 00:01:25,830 So the while loop in Rust works pretty much the same way 28 00:01:25,830 --> 00:01:27,663 as it does in any other language. 29 00:01:28,560 --> 00:01:29,850 I guess the only thing you've gotta be 30 00:01:29,850 --> 00:01:31,110 a little bit careful about 31 00:01:31,110 --> 00:01:35,970 is if you wanna have like an iteration with a loop counter, 32 00:01:35,970 --> 00:01:37,800 remember that the loop counter 33 00:01:37,800 --> 00:01:40,020 has to be declared as mutable, 34 00:01:40,020 --> 00:01:42,480 otherwise you can't change it. 35 00:01:42,480 --> 00:01:44,310 So in this example here, 36 00:01:44,310 --> 00:01:47,400 I've declared a mutuable loop counter i 37 00:01:47,400 --> 00:01:51,090 and while i is less than 10, execute the body. 38 00:01:51,090 --> 00:01:52,560 It prints the value of i 39 00:01:52,560 --> 00:01:55,830 and then it increments i ready for the next iteration. 40 00:01:55,830 --> 00:01:58,560 Just like the if statement, when you have a while loop, 41 00:01:58,560 --> 00:02:00,240 when you have any loop, in fact, 42 00:02:00,240 --> 00:02:04,650 you've got to use braces to delimit the beginning 43 00:02:04,650 --> 00:02:05,910 and end of the body. 44 00:02:05,910 --> 00:02:08,040 Even if there was just one statement, you'd still need 45 00:02:08,040 --> 00:02:09,690 to have the curly brackets. 46 00:02:09,690 --> 00:02:13,170 And obviously again, the test here 47 00:02:13,170 --> 00:02:16,410 has to be a Boolean expression 48 00:02:16,410 --> 00:02:19,470 and you don't put it in parentheses. 49 00:02:19,470 --> 00:02:22,830 So the kind of rules that we learned earlier still apply. 50 00:02:22,830 --> 00:02:24,813 So that's the basic while loop. 51 00:02:25,740 --> 00:02:28,890 Just a quick reminder that Rust doesn't have a plus plus 52 00:02:28,890 --> 00:02:30,900 and a minus minus operator. 53 00:02:30,900 --> 00:02:33,780 So if you attempted to say, I++, don't, 54 00:02:33,780 --> 00:02:36,630 it won't work, you'd have to use this kind of syntax, 55 00:02:36,630 --> 00:02:37,983 i+ equals one. 56 00:02:40,020 --> 00:02:42,120 So that's the basic while loop. 57 00:02:42,120 --> 00:02:44,790 And then we have a for-in loop. 58 00:02:44,790 --> 00:02:48,150 Iterate through all elements in a collection. 59 00:02:48,150 --> 00:02:51,540 So in this example here, I've declared an array. 60 00:02:51,540 --> 00:02:54,060 We haven't looked at arrays, but I'm sure you can imagine 61 00:02:54,060 --> 00:02:55,140 what's going on here. 62 00:02:55,140 --> 00:02:58,560 I've declared an array of five integers. 63 00:02:58,560 --> 00:03:03,150 Maybe these are exam marks, and this is my array variable. 64 00:03:03,150 --> 00:03:05,730 I've called it arr, array 65 00:03:05,730 --> 00:03:08,850 and iterate for each element in the array. 66 00:03:08,850 --> 00:03:11,580 So 99, each value in the array 67 00:03:11,580 --> 00:03:14,640 will be copied into this variable. 68 00:03:14,640 --> 00:03:16,800 I called it elem, as in element. 69 00:03:16,800 --> 00:03:18,750 You can call it anything you want. 70 00:03:18,750 --> 00:03:22,500 The value of 99 will be passed into here, then 55, 71 00:03:22,500 --> 00:03:24,630 then 95 you can imagine. 72 00:03:24,630 --> 00:03:27,180 And in this example, each time on the loop, 73 00:03:27,180 --> 00:03:31,140 I print the value of the element and then it carries on. 74 00:03:31,140 --> 00:03:33,750 So that's fairly straightforward syntax. 75 00:03:33,750 --> 00:03:35,610 Even though it might be different from what you've seen 76 00:03:35,610 --> 00:03:40,383 in other languages, it kind of makes sense. 77 00:03:41,310 --> 00:03:43,890 Just one word of caution. 78 00:03:43,890 --> 00:03:45,360 Rust doesn't have the kind 79 00:03:45,360 --> 00:03:47,790 of traditional for loop where you'd see 80 00:03:47,790 --> 00:03:49,440 in other languages where you'd say 81 00:03:49,440 --> 00:03:52,650 like initialization, semicolon, some kind 82 00:03:52,650 --> 00:03:56,730 of test condition, semicolon, and then an updated term. 83 00:03:56,730 --> 00:03:59,040 Rust doesn't have that syntax 84 00:03:59,040 --> 00:04:02,133 it just has the for-in syntax, which I'm showing you here. 85 00:04:03,450 --> 00:04:06,180 So if you wanted to iterate 10 times, 86 00:04:06,180 --> 00:04:10,650 then you can use for-in to iterate over a numeric range. 87 00:04:10,650 --> 00:04:11,940 It would look something like this. 88 00:04:11,940 --> 00:04:15,990 In this first example, I specified an exclusive upper limit. 89 00:04:15,990 --> 00:04:20,040 Remember we saw this syntax earlier when we were looking 90 00:04:20,040 --> 00:04:22,230 at match expressions. 91 00:04:22,230 --> 00:04:25,920 So if you write like a number, dot, dot, number, 92 00:04:25,920 --> 00:04:29,670 that gives you a range inclusive on the lower bound, 93 00:04:29,670 --> 00:04:31,710 exclusive on the upper bound. 94 00:04:31,710 --> 00:04:35,550 So effectively, this is gonna print out zero through to nine 95 00:04:35,550 --> 00:04:36,570 each time around the loop. 96 00:04:36,570 --> 00:04:39,330 So this basically generates the number sequence zero 97 00:04:39,330 --> 00:04:42,720 through to nine, and then those numbers are fed into i 98 00:04:42,720 --> 00:04:44,520 or whatever you wanna call it 99 00:04:44,520 --> 00:04:46,743 to iterate from zero up to nine. 100 00:04:47,670 --> 00:04:49,860 If you want to have an inclusive upper limit, 101 00:04:49,860 --> 00:04:54,000 then you use the equals sign here. 102 00:04:54,000 --> 00:04:56,520 So in the upper example, 103 00:04:56,520 --> 00:04:58,680 it would give me an exclusive upper limit. 104 00:04:58,680 --> 00:05:01,230 It would give me effectively zero to nine 105 00:05:01,230 --> 00:05:03,840 and this one, it would give me zero to 10, 106 00:05:03,840 --> 00:05:06,180 including the upper limit like so. 107 00:05:06,180 --> 00:05:10,770 So it's interesting that in Rust when you have a for loop, 108 00:05:10,770 --> 00:05:13,770 you can specify an exclusive limit 109 00:05:13,770 --> 00:05:16,560 but you can't do it when you're using a match statement. 110 00:05:16,560 --> 00:05:18,710 So that's an interesting discrepancy there. 111 00:05:20,130 --> 00:05:22,140 Okay, so there we go. 112 00:05:22,140 --> 00:05:23,820 So that's how you basically do a... 113 00:05:23,820 --> 00:05:25,800 This would be equivalent. 114 00:05:25,800 --> 00:05:27,840 This one here would be the equivalent 115 00:05:27,840 --> 00:05:30,030 in kind of other languages to something like this. 116 00:05:30,030 --> 00:05:35,030 For int i equals zero, i less than 10 i++. 117 00:05:40,470 --> 00:05:44,283 That would be this is the Rust way of doing that. 118 00:05:45,120 --> 00:05:48,270 Okay, a quick example just to show this in practice. 119 00:05:48,270 --> 00:05:52,290 Same project folders as before, lesson03_flow_control. 120 00:05:52,290 --> 00:05:55,650 It's the demo_loops function we're gonna look at this time. 121 00:05:55,650 --> 00:05:56,790 Okay, well, here we are. 122 00:05:56,790 --> 00:05:59,100 Here's the code in the src folder. 123 00:05:59,100 --> 00:06:03,950 Here's our main application, uncomment demo_loops. 124 00:06:05,190 --> 00:06:09,423 And let's have a look at that function. 125 00:06:10,830 --> 00:06:12,900 And again, I'm gonna run it in a terminal window 126 00:06:12,900 --> 00:06:15,240 so that as I'm kind of discussing it, 127 00:06:15,240 --> 00:06:16,590 the code is already there, 128 00:06:16,590 --> 00:06:19,650 ready for us to look at the output. 129 00:06:19,650 --> 00:06:20,643 Cargo run. 130 00:06:26,820 --> 00:06:30,810 So if I just scroll up a little bit, 131 00:06:30,810 --> 00:06:33,030 that's the beginning of the useful bit. 132 00:06:33,030 --> 00:06:37,530 So here, notice, I've got an infinite loop. 133 00:06:37,530 --> 00:06:39,000 I've commented it out though 134 00:06:39,000 --> 00:06:41,940 because if I uncommented it, 135 00:06:41,940 --> 00:06:43,773 then it would loop round forever. 136 00:06:45,120 --> 00:06:46,800 So I'll run this 137 00:06:46,800 --> 00:06:48,690 in a moment actually just to show what it does. 138 00:06:48,690 --> 00:06:52,710 So it would print out, this loop will go on forever. 139 00:06:52,710 --> 00:06:54,930 Hit Control + C to stop me. 140 00:06:54,930 --> 00:06:59,930 And then std::thread::sleep, it pauses, 141 00:07:00,300 --> 00:07:04,050 it sleeps the current thread for a duration. 142 00:07:04,050 --> 00:07:06,690 Obviously there's syntax here that we haven't discussed yet 143 00:07:06,690 --> 00:07:08,460 but you can guess what's going on, can't you? 144 00:07:08,460 --> 00:07:11,403 It's gonna basically take a duration of one second. 145 00:07:12,840 --> 00:07:14,160 The current thread will sleep 146 00:07:14,160 --> 00:07:17,190 for one second and then it'll repeat. 147 00:07:17,190 --> 00:07:19,470 And this thing will repeat forever. 148 00:07:19,470 --> 00:07:21,120 Because it's an infinite loop, 149 00:07:21,120 --> 00:07:22,590 this will never terminate. 150 00:07:22,590 --> 00:07:25,110 So it'll just keep on going and going and going, 151 00:07:25,110 --> 00:07:27,750 which is why I commented it out initially. 152 00:07:27,750 --> 00:07:32,050 So if I ran it, if I save this file now 153 00:07:33,270 --> 00:07:36,270 and then run it again, it's just gonna loop forever, 154 00:07:36,270 --> 00:07:39,570 display in a message every one second. 155 00:07:39,570 --> 00:07:42,900 It'll display a message and it'll keep on going. 156 00:07:42,900 --> 00:07:45,720 So you can see here I'm in an infinite loop. 157 00:07:45,720 --> 00:07:47,520 This loop is iterating forever. 158 00:07:47,520 --> 00:07:50,550 I could be doing some kind of event dispatch mechanism here 159 00:07:50,550 --> 00:07:53,190 but in this case, it's never gonna terminate. 160 00:07:53,190 --> 00:07:55,590 Hit Control + C to stop me. 161 00:07:55,590 --> 00:07:57,363 Control + C stopped it. 162 00:07:59,460 --> 00:08:01,200 So that's why it was commented out 163 00:08:01,200 --> 00:08:03,720 because otherwise, we're not gonna get very far 164 00:08:03,720 --> 00:08:04,553 with the code. 165 00:08:04,553 --> 00:08:07,710 Oh, incidentally, in the next section, we're going to look 166 00:08:07,710 --> 00:08:10,170 at the break keyword and the continue keyword. 167 00:08:10,170 --> 00:08:13,020 So that would be one way of getting out of an infinite loop. 168 00:08:13,020 --> 00:08:14,370 We'll see that a bit later. 169 00:08:15,240 --> 00:08:17,220 Anyway, so I've commented that out. 170 00:08:17,220 --> 00:08:20,040 So it's not gonna impinge us any further. 171 00:08:20,040 --> 00:08:21,630 When I run the application, 172 00:08:21,630 --> 00:08:23,613 I've got the example of a while loop. 173 00:08:26,280 --> 00:08:29,190 So the while loop goes, I've got my mutable counter. 174 00:08:29,190 --> 00:08:32,760 It goes from zero, less than 10, so that's gonna be nine, 175 00:08:32,760 --> 00:08:35,580 zero through to nine, which is what we see there. 176 00:08:35,580 --> 00:08:36,480 Very good. 177 00:08:36,480 --> 00:08:38,670 And then I've got a for loop which will iterate 178 00:08:38,670 --> 00:08:40,470 over the elements in the array. 179 00:08:40,470 --> 00:08:42,840 So that's the for loop here. 180 00:08:42,840 --> 00:08:44,700 That's great. 181 00:08:44,700 --> 00:08:47,640 And I've got a for loop over a numerical range 182 00:08:47,640 --> 00:08:51,420 with an exclusive upper limit up to, but not including 10. 183 00:08:51,420 --> 00:08:54,060 So zero to nine, in other words. 184 00:08:54,060 --> 00:08:56,910 That's what we have here, zero through to nine. 185 00:08:56,910 --> 00:08:59,853 And then using an inclusive upper limit, 186 00:09:00,840 --> 00:09:02,853 including the upper limit 10, 187 00:09:03,690 --> 00:09:06,633 then it gives us the output here, zero through to 10. 188 00:09:08,160 --> 00:09:10,470 So those are the three keywords that we have 189 00:09:10,470 --> 00:09:13,650 in Rust for writing a loop. 190 00:09:13,650 --> 00:09:16,140 The loop keyword, which loops forever, 191 00:09:16,140 --> 00:09:21,140 the while eyword and then the for-in mechanism. 192 00:09:21,150 --> 00:09:24,420 Okay, so that's the end of this lesson, this section. 193 00:09:24,420 --> 00:09:26,040 What we'll do in the next section is have a look 194 00:09:26,040 --> 00:09:28,350 at the break keyword and the continue keyword 195 00:09:28,350 --> 00:09:31,953 to see how to influence execution over a loop.