Skip to content

heavypackets/rust-iterator-emoji

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Iterator Methods

Basic

cloned

[๐Ÿฎ, ๐Ÿฅ”, ๐Ÿ”, ๐ŸŒฝ]
    .iter()
    .cloned()
    .collect::<Vec<_>>() // [๐Ÿฎ, ๐Ÿฅ”, ๐Ÿ”, ๐ŸŒฝ]

collect

[๐Ÿฎ, ๐Ÿฅ”, ๐Ÿ”, ๐ŸŒฝ]
    .iter()
    .collect::<Vec<_>>() // [&๐Ÿฎ, &๐Ÿฅ”, &๐Ÿ”, &๐ŸŒฝ]

count

[๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ].iter().count() // 3 (usize)

last

[๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ].iter().last() // &๐Ÿฆ 

nth

[๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ].iter().nth(1) // &๐Ÿถ

Behavior

cycle

list = [๐Ÿฎ, ๐Ÿฅ”, ๐Ÿ”, ๐ŸŒฝ]
    .iter()
    .cycle()
    .take(6)
    .collect::<Vec<_>>() // [&๐Ÿฎ, &๐Ÿฅ”, &๐Ÿ”, &๐ŸŒฝ, &๐Ÿฎ, &๐Ÿฅ”]

rev

[๐Ÿฎ, ๐Ÿฅ”, ๐Ÿ”, ๐ŸŒฝ]
    .iter()
    .rev()
    .collect::<Vec<_>>() // [&๐ŸŒฝ, &๐Ÿ”, &๐Ÿฅ”, &๐Ÿฎ]

skip

[๐Ÿฎ, ๐Ÿฅ”, ๐Ÿ”, ๐ŸŒฝ]
    .iter()
    .skip(2)
    .collect::<Vec<_>>() // [&๐Ÿ”, &๐ŸŒฝ]

step_by

[๐Ÿฎ, ๐Ÿฅ”, ๐Ÿ”, ๐ŸŒฝ]
    .iter()
    .step_by(2)
    .collect::<Vec<_>>() // [&๐Ÿฎ, &๐Ÿ”]

take

list = [๐Ÿฎ, ๐Ÿฅ”, ๐Ÿ”, ๐ŸŒฝ]
    .iter()
    .take(3)
    .collect::<Vec<_>>() // [&๐Ÿฎ, &๐Ÿฅ”, &๐Ÿ”]

Combine

chain

let animals = [๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ];
let food = [๐Ÿฐ, ๐Ÿ”, ๐Ÿฌ];
animals.iter()
    .chain(food.iter())
    .collect::<Vec<_>>() // [&๐Ÿฑ, &๐Ÿถ, &๐Ÿฆ, &๐Ÿฐ, &๐Ÿ”, &๐Ÿฌ]

flatten

vec![vec![๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ], vec![๐Ÿฐ, ๐Ÿ”, ๐Ÿฌ]]
    .iter()
    .flatten()
    .collect::<Vec<_>>() // [&๐Ÿฑ, &๐Ÿถ, &๐Ÿฆ, &๐Ÿฐ, &๐Ÿ”, &๐Ÿฌ]

unzip

let (animals, foods): (Vec<Animal>, Vec<Food>) = 
[(๐Ÿฑ, ๐Ÿฐ), (๐Ÿถ, ๐Ÿ”), (๐Ÿฆ, ๐Ÿฌ)]
    .iter()
    .unzip(food.iter());

println!("{:?}", left); // [๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ]
println!("{:?}", right); // [๐Ÿฐ, ๐Ÿ”, ๐Ÿฌ]

zip

let animals = [๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ];
let food = [๐Ÿฐ, ๐Ÿ”, ๐Ÿฌ];
animals.iter()
    .zip(food.iter())
    .collect::<Vec<_>>() // [(&๐Ÿฑ, &๐Ÿฐ), (&๐Ÿถ, &๐Ÿ”), (&๐Ÿฆ, &๐Ÿฌ)]

Recursive

emumerate

[๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ]
    .iter()
    .enumerate()
    .collect::<Vec<_>>() // [(0, &๐Ÿฑ), (1, &๐Ÿถ), (2, &๐Ÿฆ)]
for (index, animal) in [๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ].iter().enumerate() 
{
    println!("{}, {}", index, animal) // "0, ๐Ÿฑ ", "1, ๐Ÿถ ", "2, ๐Ÿฆ "
}

flat_map

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?

fold

[๐Ÿ”, ๐ŸŸ, ๐Ÿจ] // calories: 500, 200, 600
    .iter()
    .fold(0, |total_calories, &food| {
        total_calories + food
    }) // 1300

for_each

[๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ]
    .iter()
    .for_each(|&animal| {
        speak(animal) // "meow", "bark", "chirp"
    });

map

[๐Ÿฎ, ๐Ÿฅ”, ๐Ÿ”, ๐ŸŒฝ]
    .iter()
    .map(|&ingredient| cook(ingredient))
    .collect::<Vec<_>>() // [๐Ÿ”, ๐ŸŸ, ๐Ÿ—, ๐Ÿฟ]

scan

[๐Ÿ”, ๐ŸŸ, ๐Ÿจ] // calories: 500, 200, 600
    .iter()
    .scan(0, |total_calories, &food| {
        *total_calories = *total_calories + food;

        Some(total_calories)
    })
    .collect::<Vec<_>>() // [500, 700, 1300]

Search & Filter

filter

[๐Ÿ”, ๐ŸŸ, ๐Ÿ—, ๐Ÿฟ]
    .iter()
    .filter(|&food| is_vegetarian(food))
    .collect::<Vec<_>>() // [&๐ŸŸ, &๐Ÿฟ]

filter_map

[๐Ÿฎ, ๐Ÿฅ”, ๐Ÿ”, ๐ŸŒฝ]
    .iter()
    .filter_map(|&ingredient| {
        if is_vegetable(ingredient) {
            Some(cook(ingredient))
        } else {
            None
        }
    })
    .collect::<Vec<_>>() // [๐ŸŸ, ๐Ÿฟ]

skip_while

[๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ, ๐Ÿฐ, ๐Ÿ”, ๐Ÿฌ, ๐Ÿ”]
    .iter()
    .skip_while(|&x| is_animal(x))
    .collect::<Vec<_>>() // [&๐Ÿฐ, &๐Ÿ”, &๐Ÿฌ, &๐Ÿ”]

take_while

[๐Ÿฑ, ๐Ÿถ, ๐Ÿฆ, ๐Ÿฐ, ๐Ÿ”, ๐Ÿฌ, ๐Ÿ”]
    .iter()
    .take_while(|&x| is_animal(x))
    .collect::<Vec<_>>() // [&๐Ÿฑ, &๐Ÿถ, &๐Ÿฆ]

partition

let (vegetarian, omnivore): <(Vec<Food>, Vec<Food>)> = [๐Ÿ”, ๐ŸŸ, ๐Ÿ—, ๐Ÿฟ]
    .iter()
    .partition(|food| is_vegetarian(food));
    
println!("{:?}", vegetarian); // [&๐ŸŸ, &๐Ÿฟ]
println!("{:?}", omnivore); // [&๐Ÿ”, &๐Ÿ—]

all

[๐Ÿ”, ๐ŸŸ, ๐Ÿ—, ๐Ÿฟ]
    .iter()
    .all(|&food| is_vegetarian(food)) // false
[๐Ÿฅ—, ๐Ÿ‡, ๐Ÿ…, ๐ŸŽ]
    .iter()
    .all(|&food| is_vegetarian(food)) // true

any

[๐Ÿ”, ๐ŸŸ, ๐Ÿ—, ๐Ÿฟ]
    .iter()
    .any(|&food| is_vegetarian(food)) // true
[๐Ÿ”, ๐Ÿ—, ๐Ÿฃ, ๐Ÿฅ“]
    .iter()
    .any(|&food| is_vegetarian(food)) // false

find

[๐Ÿ”, ๐ŸŸ, ๐Ÿ—, ๐Ÿฟ]
    .iter()
    .find(|&food| is_vegetarian(food)) // Some(&๐ŸŸ)
[๐Ÿ”, ๐Ÿ—, ๐Ÿฃ, ๐Ÿฅ“]
    .iter()
    .find(|&food| is_vegetarian(food)) // None

find_map

[๐Ÿฎ, ๐Ÿฅ”, ๐Ÿ”, ๐ŸŒฝ]
    .iter()
    .find_map(|ingredient| {
        if is_vegetable(ingredient) {
            Some(ingredient)
        } else {
            None
        }
    }) // &๐Ÿฅ” 

position

[๐Ÿ”, ๐ŸŸ, ๐Ÿ—, ๐Ÿฟ]
    .iter()
    .position(|&food| is_vegetarian(food)) // Some(1)
[๐Ÿ”, ๐Ÿ—, ๐Ÿฃ, ๐Ÿฅ“]
    .iter()
    .position(|&food| is_vegetarian(food)) // None

Releases

No releases published

Packages

No packages published

Languages