[No Audio]
This tutorial is about for loops, which are
used when we know the number of times a loop
will be executed. One of the key uses of the
for loop, is to iterate or go through the
elements of array or vector. Let me declare a vector.
[No Audio]
Next to print the elements of the
array one by one, I will use a for loop.
In this case now, I know in advance that there
are 6 elements in the vector. So I will use
a for loop using the keyword of for, and
then I will mention the iterating variable
which will be incremented in each of the
iteration itself. Programmers by convention
use the variable i in connection with a for loop.
[No Audio]
Next, we will mention the values
variable I will be assuming. The syntax for
that is to write i in, and then the range of
values which, in this case will be from 0 to 5.
[No Audio]
Now in the first iteration, the
variable i will have a value of 0, and then
in subsequent iterations, its value will be
incremented by one each time.
Inside the body of the loop,
I will simply print the values
of the variable i.
[No Audio]
Let us execute this.
[No Audio]
During each iteration of
the loop, we have the value of the variable
i being printed on the terminal, starting
from the value of 0 and ending at a value of 5.
Let us now use the variable i to
print the values of the vector.
[No Audio]
In this case, the first placeholder will have
the values of the variable i, and the second
place holder will have the ith element of the vector.
[No Audio]
When the loop executes for the
first time, the variable i will have a value of 0.
And therefore the print statement
will display, the first value inside the
vector, having a value of 45.
[No Audio]
In subsequent iterations,
the values will be displayed in
sequential order the same way.
Let us execute this.
[No Audio]
We can also iterate through all the elements
in and understand. Instead of mentioning the
values of 0..5, we can write
something like, for i in some_vec.
And then inside the print statement, I will update
the print command, and we'll print the values of i.
[No Audio]
In this case now, the variable i will be
assuming values from the vector in sequential
order, let us cargo run this to confirm.
[No Audio]
Now, here is an important point to note
before we go further, the value of some_vec
are being consumed inside the
loop, and if we want to access the vector
outside the loop, the compiler will give us
an error. Let me add a print statement, where I
will access the vector.
[No Audio]
You may note a red line, it says borrowed
of a moved value. In fact, when we write for
i in some_vec, so the values
are moved from the vector to the variable i,
and therefore changes the ownership.
Moreover, the scope of the variable i is also
limited to the body of the for loop. And if I
write a variable i instead of some_vec
in the print statement, it will also
complain, saying that cannot find i in the
scope. This means that we have to be very
careful when using this syntax.
If we do not want this to happen, but rather we want a
variable i to use the borrowed values, we
will use the iter function. This is quite a
handy function and it has many useful usages,
which will become more evident to us, when we
cover the iterators in the upcoming section.
We can call this function on the vector, and
it will enable the borrowing of each element
of the collection through each iteration,
without changing the ownership. If we include
now the println statement afterwards,
the compiler won't complain, because the ownership
remains with some_vec variable.
However, please note that the variable i is
in the scope of the loop only, and is not
known outside the body of the loop.
[No Audio]
You may recall from the previous section of the
course, that we can use the & sign also for
referencing and borrowing of elements.
This means that instead of using the .iter
function, which basically borrows the
elements one by one, we may also use the & sign
which will exactly do the same job.
[No Audio]
Let us cargo run to confirm.
[No Audio]
Please note that, when I write some_vec
without the & sign, so the type of the
variable i is that of i32, which
suggests actual owned value and not references.
When I change it to some_vec.iter,
then the type of the variable i is
changed to a reference to i32, that is an
i32 with an & sign. This updation of
the types of variables will become very handy to us,
as we tend to move along in Rust programming.
In the previous section of the
course, we covered two types of references
called immutable references and mutable references.
The syntax that we have just seen,
allows for immutable references of vector values.
What if we want to have mutable
references to all the values of the vector?
To have mutable references, we will use
iter_mut function, it allows for
mutable references of the vector. This means
that, we can now use the variable i to update
the values of the vector inside the loop,
inside the for loop, without transferring the ownership.
Let me update all the values inside the loop.
[No Audio]
Okay, let us stop here for a while, because we
have encountered something that is unusual.
Let me explain the meaning of the star in
this case. The star is called a deref
operator, and when we use a variable as a
mutable variable, so the value itself is like
a pointer and not the actual value. So, to
update the actual value of the variable which
it is pointing to, we will use a star
operator. In summary for updating, or
accessing a value of a variable which is
behind a mutable reference, we will always
use the star. Please note again, that in the
print command, we are not doing any
processing, there is updation or computation
on the values therefore the Rust syntax do
not require the deref operator in this
case, let us cargo run this.
[No Audio]
The array has been updated, and the values are
being printed. Please note that the variable
i type, in this case, is set to a mutable
reference of type i32. This gives us a
useful hint that, instead of using this
function, we may write something like &mut
some_vec, and the code will still
work in the very same way. Because &mut
before the some_vec, creates the
immutable reference for the vector.
[No Audio]
Let us execute this to check.
[No Audio]
Okay, last but not the least, the for loop
can be used to iterate through the
elements of other types of collections also,
such as HashMaps, HashSets, and others. The
details of those will come later on, as we
progress in the course. That brings us to the
end of this tutorial. There are very few
topics left in this section of the course,
after which we will be covering a bit more
advanced topics in Rust. I hope you will be
enjoying the course. So do come back again
with more fun and exciting tutorials. And
until next tutorial, happy Rust programming.
[No Audio]