- So far in this lesson,
we've seen how to write if statements
and we've seen how to use the match keyword
to do decision making.
And what we're going to do in this section
is to see how to write loops.
So Rust has several different ways to write a loop.
The first keyword we're going to look at is loop.
As you can see here,
it's an infinite loop basically, so it'll loop forever.
That might be useful in a real-time system
where you wanna have like an event-driven application.
You have a loop, which is continuously iterating,
waiting for an event to arrive on some kind of queue,
dispatch the event, and then loop back again,
ready to get the next item from the event queue.
So this is an infinite loop in Rust.
If you were a C++
or a C# or Java kind of developer,
then you could have achieved the same effect
right in the for loop like this.
Like so.
That doesn't work in Rust.
You have to do it the way I've shown you here.
So that's an infinite loop.
And then next up there's a while loop.
So the while loop in Rust works pretty much the same way
as it does in any other language.
I guess the only thing you've gotta be
a little bit careful about
is if you wanna have like an iteration with a loop counter,
remember that the loop counter
has to be declared as mutable,
otherwise you can't change it.
So in this example here,
I've declared a mutuable loop counter i
and while i is less than 10, execute the body.
It prints the value of i
and then it increments i ready for the next iteration.
Just like the if statement, when you have a while loop,
when you have any loop, in fact,
you've got to use braces to delimit the beginning
and end of the body.
Even if there was just one statement, you'd still need
to have the curly brackets.
And obviously again, the test here
has to be a Boolean expression
and you don't put it in parentheses.
So the kind of rules that we learned earlier still apply.
So that's the basic while loop.
Just a quick reminder that Rust doesn't have a plus plus
and a minus minus operator.
So if you attempted to say, I++, don't,
it won't work, you'd have to use this kind of syntax,
i+ equals one.
So that's the basic while loop.
And then we have a for-in loop.
Iterate through all elements in a collection.
So in this example here, I've declared an array.
We haven't looked at arrays, but I'm sure you can imagine
what's going on here.
I've declared an array of five integers.
Maybe these are exam marks, and this is my array variable.
I've called it arr, array
and iterate for each element in the array.
So 99, each value in the array
will be copied into this variable.
I called it elem, as in element.
You can call it anything you want.
The value of 99 will be passed into here, then 55,
then 95 you can imagine.
And in this example, each time on the loop,
I print the value of the element and then it carries on.
So that's fairly straightforward syntax.
Even though it might be different from what you've seen
in other languages, it kind of makes sense.
Just one word of caution.
Rust doesn't have the kind
of traditional for loop where you'd see
in other languages where you'd say
like initialization, semicolon, some kind
of test condition, semicolon, and then an updated term.
Rust doesn't have that syntax
it just has the for-in syntax, which I'm showing you here.
So if you wanted to iterate 10 times,
then you can use for-in to iterate over a numeric range.
It would look something like this.
In this first example, I specified an exclusive upper limit.
Remember we saw this syntax earlier when we were looking
at match expressions.
So if you write like a number, dot, dot, number,
that gives you a range inclusive on the lower bound,
exclusive on the upper bound.
So effectively, this is gonna print out zero through to nine
each time around the loop.
So this basically generates the number sequence zero
through to nine, and then those numbers are fed into i
or whatever you wanna call it
to iterate from zero up to nine.
If you want to have an inclusive upper limit,
then you use the equals sign here.
So in the upper example,
it would give me an exclusive upper limit.
It would give me effectively zero to nine
and this one, it would give me zero to 10,
including the upper limit like so.
So it's interesting that in Rust when you have a for loop,
you can specify an exclusive limit
but you can't do it when you're using a match statement.
So that's an interesting discrepancy there.
Okay, so there we go.
So that's how you basically do a...
This would be equivalent.
This one here would be the equivalent
in kind of other languages to something like this.
For int i equals zero, i less than 10 i++.
That would be this is the Rust way of doing that.
Okay, a quick example just to show this in practice.
Same project folders as before, lesson03_flow_control.
It's the demo_loops function we're gonna look at this time.
Okay, well, here we are.
Here's the code in the src folder.
Here's our main application, uncomment demo_loops.
And let's have a look at that function.
And again, I'm gonna run it in a terminal window
so that as I'm kind of discussing it,
the code is already there,
ready for us to look at the output.
Cargo run.
So if I just scroll up a little bit,
that's the beginning of the useful bit.
So here, notice, I've got an infinite loop.
I've commented it out though
because if I uncommented it,
then it would loop round forever.
So I'll run this
in a moment actually just to show what it does.
So it would print out, this loop will go on forever.
Hit Control + C to stop me.
And then std::thread::sleep, it pauses,
it sleeps the current thread for a duration.
Obviously there's syntax here that we haven't discussed yet
but you can guess what's going on, can't you?
It's gonna basically take a duration of one second.
The current thread will sleep
for one second and then it'll repeat.
And this thing will repeat forever.
Because it's an infinite loop,
this will never terminate.
So it'll just keep on going and going and going,
which is why I commented it out initially.
So if I ran it, if I save this file now
and then run it again, it's just gonna loop forever,
display in a message every one second.
It'll display a message and it'll keep on going.
So you can see here I'm in an infinite loop.
This loop is iterating forever.
I could be doing some kind of event dispatch mechanism here
but in this case, it's never gonna terminate.
Hit Control + C to stop me.
Control + C stopped it.
So that's why it was commented out
because otherwise, we're not gonna get very far
with the code.
Oh, incidentally, in the next section, we're going to look
at the break keyword and the continue keyword.
So that would be one way of getting out of an infinite loop.
We'll see that a bit later.
Anyway, so I've commented that out.
So it's not gonna impinge us any further.
When I run the application,
I've got the example of a while loop.
So the while loop goes, I've got my mutable counter.
It goes from zero, less than 10, so that's gonna be nine,
zero through to nine, which is what we see there.
Very good.
And then I've got a for loop which will iterate
over the elements in the array.
So that's the for loop here.
That's great.
And I've got a for loop over a numerical range
with an exclusive upper limit up to, but not including 10.
So zero to nine, in other words.
That's what we have here, zero through to nine.
And then using an inclusive upper limit,
including the upper limit 10,
then it gives us the output here, zero through to 10.
So those are the three keywords that we have
in Rust for writing a loop.
The loop keyword, which loops forever,
the while eyword and then the for-in mechanism.
Okay, so that's the end of this lesson, this section.
What we'll do in the next section is have a look
at the break keyword and the continue keyword
to see how to influence execution over a loop.