pub fn build_array_result<T, E, F: FnMut(usize) -> Result<T, E>, const N: usize>(
f: F,
) -> Result<[T; N], E>
Expand description
Build an array with a function that creates elements based on their value, short-circuiting if
any index returns an Err
let success: Result<_, ()> = build_array_result(|i| Ok(i * 2));
assert_eq!(success, Ok([0, 2, 4]));
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.
This is similar to the nightly-only core::array::try_from_fn