foreach

Function foreach 

Source
pub fn foreach<T, F>(collection: &[T], iteratee: F)
where F: FnMut(&T, usize),
Expand description

Execute a function on each item in a collection.

This function iterates over a collection, applying the provided iteratee function to each item along with its index.

§Arguments

  • collection - A slice of items.
  • iteratee - A function that takes a reference to an item and its index.

§Examples

use lowdash::foreach;
let numbers = vec![1, 2, 3, 4, 5];
let mut sum = 0;
foreach(&numbers, |x, _| sum += x);
assert_eq!(sum, 15);
use lowdash::foreach;

#[derive(Debug, PartialEq)]
struct Person {
    name: String,
    age: u32,
}

let people = vec![
    Person { name: "Alice".to_string(), age: 25 },
    Person { name: "Bob".to_string(), age: 30 },
    Person { name: "Carol".to_string(), age: 35 },
];

let mut names = Vec::new();
foreach(&people, |p, _| names.push(p.name.clone()));
assert_eq!(names, vec!["Alice", "Bob", "Carol"]);