- Rust Quick Start Guide
- Daniel Arbuckle
- 541字
- 2021-06-10 19:46:05
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.
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.
- 精通Nginx(第2版)
- Visual Studio 2012 Cookbook
- Java系統分析與架構設計
- 深入淺出Electron:原理、工程與實踐
- 簡單高效LATEX
- 算法精粹:經典計算機科學問題的Java實現
- C語言實驗指導及習題解析
- Mastering Linux Network Administration
- Python忍者秘籍
- BeagleBone Black Cookbook
- C++寶典
- Penetration Testing with the Bash shell
- Greenplum構建實時數據倉庫實踐
- Developing RESTful Web Services with Jersey 2.0
- Learning IBM Bluemix