Struct unarray::ArrayFromIter

source ·
pub struct ArrayFromIter<T, const N: usize>(pub Option<[T; N]>);
Expand description

A wrapper type to collect an Iterator into an array

let iter = vec![1, 2, 3].into_iter();
let ArrayFromIter(array) = iter.collect();

assert_eq!(array, Some([1, 2, 3]));

Since iterators don’t carry compile-time information about their length (even core::iter::ExactSizeIterator only provides this at runtime), collecting may fail if the iterator doesn’t yield exactly N elements:

use unarray::*;
let too_many = vec![1, 2, 3, 4].into_iter();
let ArrayFromIter::<i32, 3>(option) = too_many.collect();
assert!(option.is_none());

let too_few = vec![1, 2].into_iter();
let ArrayFromIter::<i32, 3>(option) = too_few.collect();
assert!(option.is_none());

Tuple Fields§

§0: Option<[T; N]>

Trait Implementations§

Creates a value from an iterator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.