Function array_walk

Source
pub fn array_walk<T>(
    array: &mut Vec<T>,
    callback: impl Fn(&mut T, usize) + 'static,
)
Expand description

Apply a user supplied function to every member of a vector

§Description

Applies the user-defined callback function to each element of the vector.

callback

Callback takes on two parameters. The vector parameter’s value being the first, and the index second.

Only the values of the vector may potentially be changed, i.e., the programmer cannot add, unset or reorder elements.

§Examples

Example #1 array_walk() example

use phpify::array::array_walk;

let mut fruits = vec![
    "lemon".to_string(),
    "orange".to_string(),
    "banana".to_string(),
    "apple".to_string(),
];

fn test_alter(item: &mut String, index: usize) {
    *item = format!("fruit: {}", *item);
}

array_walk(&mut fruits, test_alter);

assert_eq!(fruits[0], "fruit: lemon");
assert_eq!(fruits[1], "fruit: orange");
assert_eq!(fruits[2], "fruit: banana");
assert_eq!(fruits[3], "fruit: apple");