Function array_init::from_iter[][src]

pub fn from_iter<Array, I>(iter: I) -> Option<Array> where
    I: IntoIterator<Item = Array::Item>,
    Array: IsArray

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.

Without the nightly feature it is very likely that this will cause memcpys. For panic safety, we internally use NoDrop, which will ensure that panics in the initializer will not cause the array to be prematurely dropped. If you are using a Copy type, prefer using from_iter_copy since it does not need the panic safety stuff and is more likely to have no memcpys.

Examples


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

let four = [1u32,2,3,4];
let mut iter = four.iter().cloned().cycle();
let arr: [u32; 50] = array_init::from_iter_copy(iter).unwrap();