Function unarray::build_array

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

Build an array with a function that creates elements based on their index

let array: [usize; 5] = build_array(|i| i * 2);
assert_eq!(array, [0, 2, 4, 6, 8]);

If f panics, any already-initialized elements will be dropped without running their Drop implmentations, potentially creating resource leaks. Note that this is still “safe”, since Rust’s notion of “safety” doesn’t guarantee destructors are run.

For builder functions which might fail, consider using build_array_result or build_array_option