Function array_init::try_array_init
source · pub fn try_array_init<Err, F, T, const N: usize>(
initializer: F
) -> Result<[T; N], Err>where
F: FnMut(usize) -> Result<T, Err>,
Expand description
Initialize an array given an initializer expression that may fail.
The initializer is given the index (between 0 and N - 1
included) of the element, and returns a Result<T, Err>,
. It is allowed
to mutate external state; we will always initialize from lower to higher indices.
Examples
#[derive(PartialEq,Eq,Debug)]
struct DivideByZero;
fn inv(i : usize) -> Result<f64,DivideByZero> {
if i == 0 {
Err(DivideByZero)
} else {
Ok(1./(i as f64))
}
}
// If the initializer does not fail, we get an initialized array
let arr: [f64; 3] = array_init::try_array_init(|i| inv(3-i)).unwrap();
assert_eq!(arr,[1./3., 1./2., 1./1.]);
// The initializer fails
let res : Result<[f64;4], DivideByZero> = array_init::try_array_init(|i| inv(3-i));
assert_eq!(res,Err(DivideByZero));