Function array_tools::init_with_iter [−][src]
pub fn init_with_iter<T, A, I>(iter: I) -> Option<A> where
A: FixedSizeArray<T>,
I: Iterator<Item = T>,
Attempts to initialize array of T
with iterator over T
values.
- If iterator yields not enough items, this function returns
None
. - If iterator yields enough items items to fill array, this function returns
Some(array)
. - If iterator yields excessive items, this function only takes required number of items and returns
Some(array)
.
Panics
- Only if iterator's
next
method does.
Examples
use array_tools as art; let mut iter = [3i32, 2, 1, 0].iter().copied(); let result: Option<[i32; 5]> = art::init_with_iter(iter.by_ref()); assert_eq!(result, None); assert_eq!(iter.next(), None); let mut iter = [4i32, 3, 2, 1, 0].iter().copied(); let result: Option<[i32; 5]> = art::init_with_iter(iter.by_ref()); assert_eq!(result, Some([4i32, 3, 2, 1, 0])); assert_eq!(iter.next(), None); let mut iter = [5i32, 4, 3, 2, 1, 0].iter().copied(); let result: Option<[i32; 5]> = art::init_with_iter(iter.by_ref()); assert_eq!(result, Some([5i32, 4, 3, 2, 1])); assert_eq!(iter.next(), Some(0)); assert_eq!(iter.next(), None);