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.
pub fn main() {
// `n` will take the values: 1, 2, ..., 100 in each iteration
for n in 1..101 {
if n % 15 == 0 {
println!("fizzbuzz");
} else if n % 3 == 0 {
println!("fizz");
} else if n % 5 == 0 {
println!("buzz");
} else {
println!("{}", n);
}
}
}
Alternatively, a..=b can be used for a range that is inclusive on both ends.
The above can be written as:
pub fn main() {
// `n` will take the values: 1, 2, ..., 100 in each iteration
for n in 1..=100 {
if n % 15 == 0 {
println!("fizzbuzz");
} else if n % 3 == 0 {
println!("fizz");
} else if n % 5 == 0 {
println!("buzz");
} else {
println!("{}", n);
}
}
}
for and iterators
The for in construct is able to interact with an Iterator in several ways.
As discussed in the section on the Iterator trait, by default the for
loop will apply the iter function to the collection.
pub fn main() {
let names = ["Bob", "Frank", "Ferris"];
for name in names.iter() {
match name {
"Ferris" => println!("There is a rustacean among us!"),
_ => println!("Hello {}", name),
}
}
println!("names: {:?}", names);
}