for loops

for and range

The for in construct can be used to iterate through an Iterator.
One of the easiest ways to create an iterator is to use the range
notation a..b. This yields values from a (inclusive) to b
(exclusive) in steps of one.

Let’s write FizzBuzz using for instead of while.

  1. fn main() {
  2. // `n` will take the values: 1, 2, ..., 100 in each iteration
  3. for n in 1..101 {
  4. if n % 15 == 0 {
  5. println!("fizzbuzz");
  6. } else if n % 3 == 0 {
  7. println!("fizz");
  8. } else if n % 5 == 0 {
  9. println!("buzz");
  10. } else {
  11. println!("{}", n);
  12. }
  13. }
  14. }

Alternatively, a..=b can be used for a range that is inclusive on both ends.
The above can be written as:

  1. fn main() {
  2. // `n` will take the values: 1, 2, ..., 100 in each iteration
  3. for n in 1..=100 {
  4. if n % 15 == 0 {
  5. println!("fizzbuzz");
  6. } else if n % 3 == 0 {
  7. println!("fizz");
  8. } else if n % 5 == 0 {
  9. println!("buzz");
  10. } else {
  11. println!("{}", n);
  12. }
  13. }
  14. }

for and iterators

The for in construct is able to interact with an Iterator in several ways.
As discussed in with the Iterator trait, if not specified, the for
loop will apply the into_iter function on the collection provided to convert
the collection into an iterator. This is not the only means to convert a
collection into an iterator however, the other functions available include
iter and iter_mut.

These 3 functions will return different views of the data within your
collection.

  • iter - This borrows each element of the collection through each iteration.
    Thus leaving the collection untouched and available for reuse after the loop.

```rust, editable
fn main() {
let names = vec![“Bob”, “Frank”, “Ferris”];

  1. for name in names.iter() {
  2. match name {
  3. &"Ferris" => println!("There is a rustacean among us!"),
  4. _ => println!("Hello {}", name),
  5. }
  6. }

}

  1. * `into_iter` - This consumes the collection so that on each iteration the exact
  2. data is provided. Once the collection has been consumed it is no longer
  3. available for reuse as it has been 'moved' within the loop.
  4. ```rust, editable
  5. fn main() {
  6. let names = vec!["Bob", "Frank", "Ferris"];
  7. for name in names.into_iter() {
  8. match name {
  9. "Ferris" => println!("There is a rustacean among us!"),
  10. _ => println!("Hello {}", name),
  11. }
  12. }
  13. }
  • iter_mut - This mutably borrows each element of the collection, allowing for
    the collection to be modified in place.

```rust, editable
fn main() {
let mut names = vec![“Bob”, “Frank”, “Ferris”];

  1. for name in names.iter_mut() {
  2. match name {
  3. &mut "Ferris" => println!("There is a rustacean among us!"),
  4. _ => println!("Hello {}", name),
  5. }
  6. }

}
```

In the above snippets note the type of match branch, that is the key
difference in the types or iteration. The difference in type then of course
implies differing actions that are able to be performed.

See also

Iterator