Fish Touching🐟🎣

Rust Iterator

May 20, 2023

# Collect

Iterator in std::iter - Rust

pub fn capitalize_first(input: &str) -> String {
    let mut c = input.chars();
    match c.next() {
        None => String::new(),
        Some(first) =>  first.to_uppercase().collect::<String>() + c.as_str(),
    }
}

pub fn capitalize_words_string(words: &[&str]) -> String {
    words.iter().map(|word| capitalize_first(word)).collect::<String>()
}

# Fold

Iterator in std::iter - Rust

pub fn factorial(num: u64) -> u64 {
	// Initial of acc (sum in this example), then lambda
    (1..=num).fold(1, |sum, v| sum * v)
    // (1..=num).reduce(|sum, v| sum * v).unwrap()
    // (1..=num).product()
}

# Filter

Iterator in std::iter - Rust

let a = [0i32, 1, 2];
let mut iter = a.iter().filter(|x| x.is_positive());

let a = [0, 1, 2];
let mut iter = a.iter().filter(|&x| *x > 1); // both & and *

# iter() vs into_iter()