Function array_init::array_init

source ·
pub fn array_init<F, T, const N: usize>(initializer: F) -> [T; N]where
    F: FnMut(usize) -> T,
Expand description

Initialize an array given an initializer expression.

The initializer is given the index of the element. It is allowed to mutate external state; we will always initialize the elements in order.

Examples

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

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