Function array_init::from_iter

source ·
pub fn from_iter<Iterable, T, const N: usize>(
    iterable: Iterable
) -> Option<[T; N]>where
    Iterable: IntoIterator<Item = T>,
Expand description

Initialize an array given an iterator

We will iterate until the array is full or the iterator is exhausted. Returns None if the iterator is exhausted before we can fill the array.

  • Once the array is full, extra elements from the iterator (if any) won’t be consumed.

Examples

// Initialize an array from an iterator
// producing an array of [1,2,3,4] repeated

let four = [1,2,3,4];
let mut iter = four.iter().copied().cycle();
let arr: [u32; 10] = array_init::from_iter(iter).unwrap();
assert_eq!(arr, [1, 2, 3, 4, 1, 2, 3, 4, 1, 2]);