1 00:00:00,000 --> 00:00:09,200 [No Audio] 2 00:00:09,201 --> 00:00:11,533 This tutorial is about for loops, which are 3 00:00:11,534 --> 00:00:13,500 used when we know the number of times a loop 4 00:00:13,501 --> 00:00:17,100 will be executed. One of the key uses of the 5 00:00:17,101 --> 00:00:18,900 for loop, is to iterate or go through the 6 00:00:18,901 --> 00:00:22,233 elements of array or vector. Let me declare a vector. 7 00:00:22,234 --> 00:00:25,933 [No Audio] 8 00:00:25,934 --> 00:00:27,300 Next to print the elements of the 9 00:00:27,301 --> 00:00:29,400 array one by one, I will use a for loop. 10 00:00:30,000 --> 00:00:32,800 In this case now, I know in advance that there 11 00:00:32,801 --> 00:00:35,700 are 6 elements in the vector. So I will use 12 00:00:35,701 --> 00:00:39,600 a for loop using the keyword of for, and 13 00:00:39,601 --> 00:00:41,333 then I will mention the iterating variable 14 00:00:41,334 --> 00:00:43,000 which will be incremented in each of the 15 00:00:43,033 --> 00:00:46,100 iteration itself. Programmers by convention 16 00:00:46,101 --> 00:00:48,533 use the variable i in connection with a for loop. 17 00:00:48,534 --> 00:00:50,700 [No Audio] 18 00:00:50,701 --> 00:00:52,500 Next, we will mention the values 19 00:00:52,501 --> 00:00:55,100 variable I will be assuming. The syntax for 20 00:00:55,101 --> 00:00:58,400 that is to write i in, and then the range of 21 00:00:58,401 --> 00:01:01,166 values which, in this case will be from 0 to 5. 22 00:01:01,167 --> 00:01:05,099 [No Audio] 23 00:01:05,100 --> 00:01:06,300 Now in the first iteration, the 24 00:01:06,301 --> 00:01:08,800 variable i will have a value of 0, and then 25 00:01:08,801 --> 00:01:11,100 in subsequent iterations, its value will be 26 00:01:11,101 --> 00:01:12,733 incremented by one each time. 27 00:01:13,733 --> 00:01:15,000 Inside the body of the loop, 28 00:01:15,233 --> 00:01:16,600 I will simply print the values 29 00:01:16,601 --> 00:01:17,666 of the variable i. 30 00:01:17,667 --> 00:01:23,100 [No Audio] 31 00:01:23,101 --> 00:01:24,266 Let us execute this. 32 00:01:24,267 --> 00:01:28,900 [No Audio] 33 00:01:28,901 --> 00:01:30,033 During each iteration of 34 00:01:30,034 --> 00:01:32,033 the loop, we have the value of the variable 35 00:01:32,034 --> 00:01:34,200 i being printed on the terminal, starting 36 00:01:34,201 --> 00:01:36,933 from the value of 0 and ending at a value of 5. 37 00:01:37,900 --> 00:01:39,833 Let us now use the variable i to 38 00:01:39,834 --> 00:01:41,266 print the values of the vector. 39 00:01:41,267 --> 00:01:46,500 [No Audio] 40 00:01:46,501 --> 00:01:48,833 In this case, the first placeholder will have 41 00:01:48,834 --> 00:01:51,000 the values of the variable i, and the second 42 00:01:51,001 --> 00:01:53,533 place holder will have the ith element of the vector. 43 00:01:53,534 --> 00:01:55,733 [No Audio] 44 00:01:55,734 --> 00:01:57,100 When the loop executes for the 45 00:01:57,101 --> 00:01:59,400 first time, the variable i will have a value of 0. 46 00:01:59,533 --> 00:02:01,133 And therefore the print statement 47 00:02:01,134 --> 00:02:03,033 will display, the first value inside the 48 00:02:03,034 --> 00:02:04,900 vector, having a value of 45. 49 00:02:04,901 --> 00:02:07,900 [No Audio] 50 00:02:07,901 --> 00:02:09,100 In subsequent iterations, 51 00:02:09,101 --> 00:02:10,500 the values will be displayed in 52 00:02:10,501 --> 00:02:12,500 sequential order the same way. 53 00:02:12,501 --> 00:02:13,966 Let us execute this. 54 00:02:13,967 --> 00:02:19,300 [No Audio] 55 00:02:19,301 --> 00:02:22,033 We can also iterate through all the elements 56 00:02:22,034 --> 00:02:24,300 in and understand. Instead of mentioning the 57 00:02:24,301 --> 00:02:26,700 values of 0..5, we can write 58 00:02:26,701 --> 00:02:29,300 something like, for i in some_vec. 59 00:02:29,533 --> 00:02:31,733 And then inside the print statement, I will update 60 00:02:31,734 --> 00:02:34,533 the print command, and we'll print the values of i. 61 00:02:34,534 --> 00:02:40,633 [No Audio] 62 00:02:40,634 --> 00:02:42,633 In this case now, the variable i will be 63 00:02:42,634 --> 00:02:44,800 assuming values from the vector in sequential 64 00:02:44,801 --> 00:02:47,233 order, let us cargo run this to confirm. 65 00:02:47,234 --> 00:02:53,733 [No Audio] 66 00:02:53,734 --> 00:02:55,500 Now, here is an important point to note 67 00:02:55,501 --> 00:02:58,300 before we go further, the value of some_vec 68 00:02:58,301 --> 00:03:00,233 are being consumed inside the 69 00:03:00,234 --> 00:03:02,233 loop, and if we want to access the vector 70 00:03:02,234 --> 00:03:04,100 outside the loop, the compiler will give us 71 00:03:04,101 --> 00:03:06,900 an error. Let me add a print statement, where I 72 00:03:06,901 --> 00:03:08,166 will access the vector. 73 00:03:08,167 --> 00:03:14,900 [No Audio] 74 00:03:14,901 --> 00:03:17,500 You may note a red line, it says borrowed 75 00:03:17,501 --> 00:03:20,900 of a moved value. In fact, when we write for 76 00:03:20,901 --> 00:03:23,733 i in some_vec, so the values 77 00:03:23,734 --> 00:03:26,300 are moved from the vector to the variable i, 78 00:03:26,333 --> 00:03:28,500 and therefore changes the ownership. 79 00:03:28,600 --> 00:03:31,233 Moreover, the scope of the variable i is also 80 00:03:31,234 --> 00:03:34,100 limited to the body of the for loop. And if I 81 00:03:34,101 --> 00:03:36,233 write a variable i instead of some_vec 82 00:03:36,234 --> 00:03:38,700 in the print statement, it will also 83 00:03:38,701 --> 00:03:41,300 complain, saying that cannot find i in the 84 00:03:41,301 --> 00:03:43,500 scope. This means that we have to be very 85 00:03:43,501 --> 00:03:45,300 careful when using this syntax. 86 00:03:46,800 --> 00:03:49,400 If we do not want this to happen, but rather we want a 87 00:03:49,401 --> 00:03:51,900 variable i to use the borrowed values, we 88 00:03:51,901 --> 00:03:55,300 will use the iter function. This is quite a 89 00:03:55,301 --> 00:03:58,433 handy function and it has many useful usages, 90 00:03:58,500 --> 00:04:01,000 which will become more evident to us, when we 91 00:04:01,001 --> 00:04:03,466 cover the iterators in the upcoming section. 92 00:04:04,133 --> 00:04:06,133 We can call this function on the vector, and 93 00:04:06,134 --> 00:04:08,200 it will enable the borrowing of each element 94 00:04:08,201 --> 00:04:10,200 of the collection through each iteration, 95 00:04:10,433 --> 00:04:14,133 without changing the ownership. If we include 96 00:04:14,134 --> 00:04:16,300 now the println statement afterwards, 97 00:04:17,399 --> 00:04:19,500 the compiler won't complain, because the ownership 98 00:04:19,501 --> 00:04:21,500 remains with some_vec variable. 99 00:04:22,200 --> 00:04:24,600 However, please note that the variable i is 100 00:04:24,601 --> 00:04:27,600 in the scope of the loop only, and is not 101 00:04:27,601 --> 00:04:29,600 known outside the body of the loop. 102 00:04:29,601 --> 00:04:31,800 [No Audio] 103 00:04:31,801 --> 00:04:33,700 You may recall from the previous section of the 104 00:04:33,701 --> 00:04:36,400 course, that we can use the & sign also for 105 00:04:36,401 --> 00:04:38,933 referencing and borrowing of elements. 106 00:04:38,966 --> 00:04:41,400 This means that instead of using the .iter 107 00:04:41,401 --> 00:04:43,033 function, which basically borrows the 108 00:04:43,034 --> 00:04:46,200 elements one by one, we may also use the & sign 109 00:04:46,201 --> 00:04:48,400 which will exactly do the same job. 110 00:04:48,401 --> 00:04:50,600 [No Audio] 111 00:04:50,601 --> 00:04:52,066 Let us cargo run to confirm. 112 00:04:52,067 --> 00:04:58,800 [No Audio] 113 00:04:58,801 --> 00:05:00,933 Please note that, when I write some_vec 114 00:05:00,934 --> 00:05:03,233 without the & sign, so the type of the 115 00:05:03,234 --> 00:05:05,633 variable i is that of i32, which 116 00:05:05,634 --> 00:05:08,700 suggests actual owned value and not references. 117 00:05:09,233 --> 00:05:11,900 When I change it to some_vec.iter, 118 00:05:13,100 --> 00:05:14,700 then the type of the variable i is 119 00:05:14,701 --> 00:05:17,500 changed to a reference to i32, that is an 120 00:05:17,501 --> 00:05:20,833 i32 with an & sign. This updation of 121 00:05:20,834 --> 00:05:23,700 the types of variables will become very handy to us, 122 00:05:24,000 --> 00:05:26,733 as we tend to move along in Rust programming. 123 00:05:27,633 --> 00:05:28,800 In the previous section of the 124 00:05:28,801 --> 00:05:30,700 course, we covered two types of references 125 00:05:30,701 --> 00:05:34,233 called immutable references and mutable references. 126 00:05:34,500 --> 00:05:36,933 The syntax that we have just seen, 127 00:05:36,934 --> 00:05:39,833 allows for immutable references of vector values. 128 00:05:40,300 --> 00:05:41,900 What if we want to have mutable 129 00:05:41,901 --> 00:05:44,100 references to all the values of the vector? 130 00:05:45,100 --> 00:05:47,900 To have mutable references, we will use 131 00:05:47,901 --> 00:05:51,200 iter_mut function, it allows for 132 00:05:51,201 --> 00:05:53,533 mutable references of the vector. This means 133 00:05:53,534 --> 00:05:56,500 that, we can now use the variable i to update 134 00:05:56,501 --> 00:05:58,566 the values of the vector inside the loop, 135 00:05:59,033 --> 00:06:02,500 inside the for loop, without transferring the ownership. 136 00:06:02,501 --> 00:06:05,066 Let me update all the values inside the loop. 137 00:06:05,067 --> 00:06:11,800 [No Audio] 138 00:06:11,801 --> 00:06:14,100 Okay, let us stop here for a while, because we 139 00:06:14,101 --> 00:06:16,400 have encountered something that is unusual. 140 00:06:16,433 --> 00:06:18,300 Let me explain the meaning of the star in 141 00:06:18,301 --> 00:06:21,200 this case. The star is called a deref 142 00:06:21,201 --> 00:06:24,300 operator, and when we use a variable as a 143 00:06:24,301 --> 00:06:27,000 mutable variable, so the value itself is like 144 00:06:27,001 --> 00:06:29,733 a pointer and not the actual value. So, to 145 00:06:29,734 --> 00:06:32,333 update the actual value of the variable which 146 00:06:32,400 --> 00:06:34,733 it is pointing to, we will use a star 147 00:06:34,734 --> 00:06:37,000 operator. In summary for updating, or 148 00:06:37,001 --> 00:06:39,333 accessing a value of a variable which is 149 00:06:39,334 --> 00:06:41,633 behind a mutable reference, we will always 150 00:06:41,634 --> 00:06:44,800 use the star. Please note again, that in the 151 00:06:44,801 --> 00:06:46,500 print command, we are not doing any 152 00:06:46,501 --> 00:06:49,800 processing, there is updation or computation 153 00:06:49,801 --> 00:06:51,833 on the values therefore the Rust syntax do 154 00:06:51,834 --> 00:06:53,633 not require the deref operator in this 155 00:06:53,634 --> 00:06:55,300 case, let us cargo run this. 156 00:06:55,301 --> 00:07:01,800 [No Audio] 157 00:07:01,801 --> 00:07:03,933 The array has been updated, and the values are 158 00:07:03,934 --> 00:07:06,533 being printed. Please note that the variable 159 00:07:06,534 --> 00:07:09,033 i type, in this case, is set to a mutable 160 00:07:09,034 --> 00:07:11,800 reference of type i32. This gives us a 161 00:07:11,801 --> 00:07:13,700 useful hint that, instead of using this 162 00:07:13,701 --> 00:07:16,200 function, we may write something like &mut 163 00:07:16,233 --> 00:07:18,600 some_vec, and the code will still 164 00:07:18,601 --> 00:07:21,533 work in the very same way. Because &mut 165 00:07:21,600 --> 00:07:24,033 before the some_vec, creates the 166 00:07:24,034 --> 00:07:25,800 immutable reference for the vector. 167 00:07:25,801 --> 00:07:28,500 [No Audio] 168 00:07:28,501 --> 00:07:30,533 Let us execute this to check. 169 00:07:30,534 --> 00:07:39,133 [No Audio] 170 00:07:39,134 --> 00:07:41,900 Okay, last but not the least, the for loop 171 00:07:41,901 --> 00:07:43,800 can be used to iterate through the 172 00:07:43,801 --> 00:07:46,300 elements of other types of collections also, 173 00:07:46,301 --> 00:07:49,000 such as HashMaps, HashSets, and others. The 174 00:07:49,001 --> 00:07:51,400 details of those will come later on, as we 175 00:07:51,401 --> 00:07:53,700 progress in the course. That brings us to the 176 00:07:53,701 --> 00:07:55,933 end of this tutorial. There are very few 177 00:07:55,934 --> 00:07:57,633 topics left in this section of the course, 178 00:07:57,634 --> 00:07:59,533 after which we will be covering a bit more 179 00:07:59,534 --> 00:08:02,033 advanced topics in Rust. I hope you will be 180 00:08:02,034 --> 00:08:04,000 enjoying the course. So do come back again 181 00:08:04,001 --> 00:08:06,000 with more fun and exciting tutorials. And 182 00:08:06,001 --> 00:08:08,766 until next tutorial, happy Rust programming. 183 00:08:08,767 --> 00:08:14,100 [No Audio]