[−][src]Function array_tools::try_init_from_slice
pub fn try_init_from_slice<T, A>(slice: &[T]) -> Option<A> where
A: FixedSizeArray<T>,
T: Clone,
Attempts to create instance of array from slice.
- If slice length is less than array length, this function returns
None
. - If slice length is greater or equal to array length, this function returns
Some(array)
.
This function clones slice elements.
Examples
Not enough elements case.
use array_tools::try_init_from_slice; let source = [1, 2, 3, 4, 5, 6]; let maybe_array: Option<[u64; 8]> = try_init_from_slice(&source); assert_eq!(maybe_array, None);
Enough or excessive elements case.
use array_tools::try_init_from_slice; let source = [1, 2, 3, 4, 5, 6]; let maybe_array: Option<[u64; 4]> = try_init_from_slice(&source); assert_eq!(maybe_array, Some([1, 2, 3, 4]));