Function array_init::map_array_init

source ·
pub fn map_array_init<M, T, U, const N: usize>(
    source: &[U; N],
    mapper: M
) -> [T; N]where
    M: FnMut(&U) -> T,
Expand description

Initialize an array given a source array and a mapping expression. The size of the source array is the same as the size of the returned array.

The mapper is given an element from the source array and maps it to an element in the destination.

Examples

// Initialize an array of length 50 containing successive squares
let arr: [usize; 50] = array_init::array_init(|i| i * i);

// Map each usize element to a u64 element.
let u64_arr: [u64; 50] = array_init::map_array_init(&arr, |element| *element as u64);

assert!(u64_arr.iter().enumerate().all(|(i, &x)| x == (i * i) as u64));