for loops

Sometimes, while loops are exactly what we need, but the two most common needs for looping are to loop a specific number of times or to use a loop to process each element contained in an array or similar data structure. In both of these cases, for loops work better.

A for loop runs its block expression once for each value produced by an iterator. An iterator is a special kind of data value that has the job of returning a sequence of values, one at a time. An iterator for an array, for example, produces a different member of the array each time we ask it for a value.

To loop a specific number of times, we can use a for loop along with a range expression, which is an expression that produces an iterator over a sequence of numbers. Let's look at a concrete example of that:

for num in 3..7 {
println!("for loop {}", num);
}

We started off with the for keyword and then num, which is the name which is going to be given to each value that the iterator produces, one at a time, as those values are processed by the for loop. Then comes another keyword, in. Finally, the expression that produces our iterator. In this case, we have a range expression, which represents the values 3, 4, 5, and 6. Notice that 7 is not included in the range. As with counting from zero, this makes some of the math easier for the computer to do, and in this case, it makes things easier for us most of the time as well. If we want to loop seven times, we could just write 0..7.

There is a variant we could use that would include the final number in the output, should we need it: 3..=7. Just remember that if you loop through 0..=7, you're going to be running the block expression eight times.

The other time when for loops shine is when we have a collection of actual values that we want to process, as the following example:

for word in ["Hello", "world", "of", "loops"].iter() {
println!("{}", word);
}

This loop prints out each of the words in the array, each on their own line. The word name is set to the first value produced by the iterator, "Hello", and the block expression is run. Then word is set to the second value produced by the iterator, "world", and the block expression is run again. This continues until the iterator runs out of values to produce, and then stops.

Here, our iterator is producing the values stored in an array. The .iter() part of that expression is saying, basically: Arrays know how to make iterators for themselves, so ask the array to give us an iterator. We'll see more about how to implement functions that are specific to a data type in a later chapter, but for now, we just need to know that that's what the . symbol means: the thing on the right of the dot is specific to the thing on the left. We are asking the computer to run, not just any iter function, but the iter function that is associated with our array.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.216.254