Function array_utils::initialize_from[][src]

pub fn initialize_from<T, F, const OUTPUT_SIZE: usize>(f: F) -> [T; OUTPUT_SIZE] where
    T: Copy + Default,
    F: Fn(usize) -> T, 

Initialize a sized array from a closure taking the index and outputting the elements.

Generates a new sized array generated from generator closure, which turns a index into a element of the generated array.

Examples

use array_utils::initialize_from;

// Size is most of the time automatically inferred
assert_eq!(initialize_from(|index| index), [0, 1, 2, 3, 4, 5]);
assert_eq!(initialize_from(|index| 2 * index), [0, 2, 4, 6, 8, 10]);

fn get_prime(index: usize) -> usize {
    // ...snip
}

assert_eq!(initialize_from(get_prime), [2, 3, 5, 7, 9, 11]);

Panics

Only panics when the given closure f panics.