Function array_init::array_init_copy[][src]

pub fn array_init_copy<Array, F>(initializer: F) -> Array where
    Array: IsArray,
    F: FnMut(usize) -> Array::Item,
    Array::Item: Copy

Initialize an array of Copy elements 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.

This is preferred over array_init if you have a Copy type

Examples


// Initialize an array of length 10 containing
// successive squares

let arr: [u32; 50] = array_init::array_init_copy(|i| (i*i) as u32);


// Closures can also mutate state. We guarantee that they will be called
// in order from lower to higher indices.

let mut last = 1u64;
let mut secondlast = 0;
let fibonacci: [u64; 50] = array_init::array_init_copy(|_| {
    let this = last + secondlast;
    secondlast = last;
    last = this;
    this
});