map

Function map 

Source
pub fn map<T, R, F>(collection: &[T], iteratee: F) -> Vec<R>
where F: Fn(&T, usize) -> R,
Expand description

Apply a function to each item in a collection, producing a new collection of results.

This function iterates over a collection and applies the provided iteratee function to each item along with its index, collecting the results into a new vector.

§Arguments

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

§Returns

  • Vec<R> - A vector containing the results of applying iteratee to each item.

§Examples

use lowdash::map;
let numbers = vec![1, 2, 3, 4, 5];
let result = map(&numbers, |x, _| x * 2);
assert_eq!(result, vec![2, 4, 6, 8, 10]);
use lowdash::map;

#[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 names: Vec<String> = map(&people, |p, _| p.name.clone());
assert_eq!(names, vec!["Alice", "Bob", "Carol"]);