[๐ฎ, ๐ฅ, ๐, ๐ฝ]
.iter()
.cloned()
.collect::<Vec<_>>() // [๐ฎ, ๐ฅ, ๐, ๐ฝ]
[๐ฎ, ๐ฅ, ๐, ๐ฝ]
.iter()
.collect::<Vec<_>>() // [&๐ฎ, &๐ฅ, &๐, &๐ฝ]
[๐ฑ, ๐ถ, ๐ฆ].iter().count() // 3 (usize)
[๐ฑ, ๐ถ, ๐ฆ].iter().last() // &๐ฆ
[๐ฑ, ๐ถ, ๐ฆ].iter().nth(1) // &๐ถ
list = [๐ฎ, ๐ฅ, ๐, ๐ฝ]
.iter()
.cycle()
.take(6)
.collect::<Vec<_>>() // [&๐ฎ, &๐ฅ, &๐, &๐ฝ, &๐ฎ, &๐ฅ]
[๐ฎ, ๐ฅ, ๐, ๐ฝ]
.iter()
.rev()
.collect::<Vec<_>>() // [&๐ฝ, &๐, &๐ฅ, &๐ฎ]
[๐ฎ, ๐ฅ, ๐, ๐ฝ]
.iter()
.skip(2)
.collect::<Vec<_>>() // [&๐, &๐ฝ]
[๐ฎ, ๐ฅ, ๐, ๐ฝ]
.iter()
.step_by(2)
.collect::<Vec<_>>() // [&๐ฎ, &๐]
list = [๐ฎ, ๐ฅ, ๐, ๐ฝ]
.iter()
.take(3)
.collect::<Vec<_>>() // [&๐ฎ, &๐ฅ, &๐]
let animals = [๐ฑ, ๐ถ, ๐ฆ];
let food = [๐ฐ, ๐, ๐ฌ];
animals.iter()
.chain(food.iter())
.collect::<Vec<_>>() // [&๐ฑ, &๐ถ, &๐ฆ, &๐ฐ, &๐, &๐ฌ]
vec![vec![๐ฑ, ๐ถ, ๐ฆ], vec![๐ฐ, ๐, ๐ฌ]]
.iter()
.flatten()
.collect::<Vec<_>>() // [&๐ฑ, &๐ถ, &๐ฆ, &๐ฐ, &๐, &๐ฌ]
let (animals, foods): (Vec<Animal>, Vec<Food>) =
[(๐ฑ, ๐ฐ), (๐ถ, ๐), (๐ฆ, ๐ฌ)]
.iter()
.unzip(food.iter());
println!("{:?}", left); // [๐ฑ, ๐ถ, ๐ฆ]
println!("{:?}", right); // [๐ฐ, ๐, ๐ฌ]
let animals = [๐ฑ, ๐ถ, ๐ฆ];
let food = [๐ฐ, ๐, ๐ฌ];
animals.iter()
.zip(food.iter())
.collect::<Vec<_>>() // [(&๐ฑ, &๐ฐ), (&๐ถ, &๐), (&๐ฆ, &๐ฌ)]
[๐ฑ, ๐ถ, ๐ฆ]
.iter()
.enumerate()
.collect::<Vec<_>>() // [(0, &๐ฑ), (1, &๐ถ), (2, &๐ฆ)]
for (index, animal) in [๐ฑ, ๐ถ, ๐ฆ].iter().enumerate()
{
println!("{}, {}", index, animal) // "0, ๐ฑ ", "1, ๐ถ ", "2, ๐ฆ "
}
let salad = vec![๐ฅ]
let entree = vec![๐, ๐];
let dessert = vec![๐จ, ๐ฐ];
let dinner = vec![salad, entree, dessert];
let vegetarian_choices: Vec<Food> = dinner
.iter()
.flat_map(|course| {
course.iter().filter(|&food| is_vegetarian(food))
})
.collect(); // [๐ฅ, ๐, ๐ฐ, ๐จ] \TODO aren't these references?
[๐, ๐, ๐จ] // calories: 500, 200, 600
.iter()
.fold(0, |total_calories, &food| {
total_calories + food
}) // 1300
[๐ฑ, ๐ถ, ๐ฆ]
.iter()
.for_each(|&animal| {
speak(animal) // "meow", "bark", "chirp"
});
[๐ฎ, ๐ฅ, ๐, ๐ฝ]
.iter()
.map(|&ingredient| cook(ingredient))
.collect::<Vec<_>>() // [๐, ๐, ๐, ๐ฟ]
[๐, ๐, ๐จ] // calories: 500, 200, 600
.iter()
.scan(0, |total_calories, &food| {
*total_calories = *total_calories + food;
Some(total_calories)
})
.collect::<Vec<_>>() // [500, 700, 1300]
[๐, ๐, ๐, ๐ฟ]
.iter()
.filter(|&food| is_vegetarian(food))
.collect::<Vec<_>>() // [&๐, &๐ฟ]
[๐ฎ, ๐ฅ, ๐, ๐ฝ]
.iter()
.filter_map(|&ingredient| {
if is_vegetable(ingredient) {
Some(cook(ingredient))
} else {
None
}
})
.collect::<Vec<_>>() // [๐, ๐ฟ]
[๐ฑ, ๐ถ, ๐ฆ, ๐ฐ, ๐, ๐ฌ, ๐]
.iter()
.skip_while(|&x| is_animal(x))
.collect::<Vec<_>>() // [&๐ฐ, &๐, &๐ฌ, &๐]
[๐ฑ, ๐ถ, ๐ฆ, ๐ฐ, ๐, ๐ฌ, ๐]
.iter()
.take_while(|&x| is_animal(x))
.collect::<Vec<_>>() // [&๐ฑ, &๐ถ, &๐ฆ]
let (vegetarian, omnivore): <(Vec<Food>, Vec<Food>)> = [๐, ๐, ๐, ๐ฟ]
.iter()
.partition(|food| is_vegetarian(food));
println!("{:?}", vegetarian); // [&๐, &๐ฟ]
println!("{:?}", omnivore); // [&๐, &๐]
[๐, ๐, ๐, ๐ฟ]
.iter()
.all(|&food| is_vegetarian(food)) // false
[๐ฅ, ๐, ๐
, ๐]
.iter()
.all(|&food| is_vegetarian(food)) // true
[๐, ๐, ๐, ๐ฟ]
.iter()
.any(|&food| is_vegetarian(food)) // true
[๐, ๐, ๐ฃ, ๐ฅ]
.iter()
.any(|&food| is_vegetarian(food)) // false
[๐, ๐, ๐, ๐ฟ]
.iter()
.find(|&food| is_vegetarian(food)) // Some(&๐)
[๐, ๐, ๐ฃ, ๐ฅ]
.iter()
.find(|&food| is_vegetarian(food)) // None
[๐ฎ, ๐ฅ, ๐, ๐ฝ]
.iter()
.find_map(|ingredient| {
if is_vegetable(ingredient) {
Some(ingredient)
} else {
None
}
}) // &๐ฅ
[๐, ๐, ๐, ๐ฟ]
.iter()
.position(|&food| is_vegetarian(food)) // Some(1)
[๐, ๐, ๐ฃ, ๐ฅ]
.iter()
.position(|&food| is_vegetarian(food)) // None