Trait from_iter::FromIterator
source · [−]pub trait FromIterator<A>: Sized {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self;
fn try_from_iter<T: IntoIterator<Item = A>>(
iter: T
) -> Result<Self, FromIteratorError>;
fn from_iter_exact<T: IntoIterator<Item = A>>(iter: T) -> Self;
fn try_from_iter_exact<T: IntoIterator<Item = A>>(
iter: T
) -> Result<Self, FromIteratorExactError<A>>;
}
Expand description
This trait contains the from_iter and try_from_iter methods.
Example:
use from_iter::FromIterator;
let iter = (0..).map(|i| i * 2);
let array = <[i32; 8]>::from_iter(iter);
assert_eq!(array, [0, 2, 4, 6, 8, 10, 12, 14]);
Required methods
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self
This method fills an array using the given iterator. Note that it will panic if the iterator doesn’t contain enough items.
fn try_from_iter<T: IntoIterator<Item = A>>(
iter: T
) -> Result<Self, FromIteratorError>
fn try_from_iter<T: IntoIterator<Item = A>>(
iter: T
) -> Result<Self, FromIteratorError>
This method fills an array using the given iterator. If there aren’t enough items available, it will return a FromIteratorError.
fn from_iter_exact<T: IntoIterator<Item = A>>(iter: T) -> Self
fn from_iter_exact<T: IntoIterator<Item = A>>(iter: T) -> Self
This method fills an array using the given iterator. Note that it will panic if the iterator doesn’t contain enough items, or there are more elements than expected.
fn try_from_iter_exact<T: IntoIterator<Item = A>>(
iter: T
) -> Result<Self, FromIteratorExactError<A>>
fn try_from_iter_exact<T: IntoIterator<Item = A>>(
iter: T
) -> Result<Self, FromIteratorExactError<A>>
This method fills an array using the given iterator.
- If there aren’t enough items available,it will return FromIteratorExactError::NotEnoughElement.
- If there are more elements than expected, it will return FromIteratorExactError::TooManyElements. This variant contains the last value returned from the given iterator, since we consume the excessive element from the iterator (if any) in order to check if the length matches.