Function array_tools::try_init_with [−][src]
pub fn try_init_with<T, A, F, E>(initializer_fn: F) -> Result<A, E> where
A: FixedSizeArray<T>,
F: FnMut() -> Result<T, E>,
Attempts to initialize array with values provided by function.
This function behaves much like init_with
, but
unlike init_with
it expects function
that returns Result
s. If Err
is received, this
function stops immediatly and returns received Err
.
Panics
- Only panics if provided function does.
Examples
use array_tools as art; #[derive(Debug, Clone, Copy, PartialEq, Eq)] struct MyErr; let mut value: u64 = 0; let result: Result<[u64; 7], MyErr> = art::try_init_with(|| { let return_value = value; value += 1; Ok(return_value) }); assert_eq!(result, Ok([0, 1, 2, 3, 4, 5, 6])); let mut value: u64 = 0; let result: Result<[u64; 7], MyErr> = art::try_init_with(|| { let return_value = value; value += 1; if value != 3 { Ok(return_value) } else { Err(MyErr) } }); assert_eq!(result, Err(MyErr));