Trait FromIterator
pub trait FromIterator<A>: Sized
Conversion from an Iterator.
By implementing FromIterator for a type, you define how it will be
created from an iterator. This is common for types which describe a
collection of some kind.
If you want to create a collection from the contents of an iterator, the
[Iterator::collect()] method is preferred. However, when you need to
specify the container type, [FromIterator::from_iter()] can be more
readable than using a turbofish (e.g. ::<Vec<_>>()). See the
[Iterator::collect()] documentation for more examples of its use.
See also: IntoIterator.
Examples
Basic usage:
let five_fives = repeat.take;
let v = Vecfrom_iter;
assert_eq!;
Using [Iterator::collect()] to implicitly use FromIterator:
let five_fives = repeat.take;
let v: = five_fives.collect;
assert_eq!;
Using [FromIterator::from_iter()] as a more readable alternative to
[Iterator::collect()]:
use VecDeque;
let first = .;
let second = from_iter;
assert_eq!;
Implementing FromIterator for your type:
// A sample collection, that's just a wrapper over Vec<T>
;
// Let's give it some methods so we can create one and add things
// to it.
// and we'll implement FromIterator
// Now we can make a new iterator...
let iter = .into_iter;
// ... and make a MyCollection out of it
let c = from_iter;
assert_eq!;
// collect works too!
let iter = .into_iter;
let c: MyCollection = iter.collect;
assert_eq!;
Required Methods
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> SelfCreates a value from an iterator.
See the module-level documentation for more.
Examples
let five_fives = repeat.take; let v = Vecfrom_iter; assert_eq!;
Implementors
impl FromIterator<()> for ()impl<A, B, C, D, E, ExA, ExB, ExC, ExD, ExE> FromIterator<(A, B, C, D, E)> for (ExA, ExB, ExC, ExD, ExE) where ExA: Default + Extend<A>, ExB: Default + Extend<B>, ExC: Default + Extend<C>, ExD: Default + Extend<D>, ExE: Default + Extend<E>,impl<A, B, C, D, E, F, ExA, ExB, ExC, ExD, ExE, ExF> FromIterator<(A, B, C, D, E, F)> for (ExA, ExB, ExC, ExD, ExE, ExF) where ExA: Default + Extend<A>, ExB: Default + Extend<B>, ExC: Default + Extend<C>, ExD: Default + Extend<D>, ExE: Default + Extend<E>, ExF: Default + Extend<F>,impl<A, B, C, D, E, F, G, ExA, ExB, ExC, ExD, ExE, ExF, ExG> FromIterator<(A, B, C, D, E, F, G)> for (ExA, ExB, ExC, ExD, ExE, ExF, ExG) where ExA: Default + Extend<A>, ExB: Default + Extend<B>, ExC: Default + Extend<C>, ExD: Default + Extend<D>, ExE: Default + Extend<E>, ExF: Default + Extend<F>, ExG: Default + Extend<G>,impl<A, B, C, D, E, F, G, H, ExA, ExB, ExC, ExD, ExE, ExF, ExG, ExH> FromIterator<(A, B, C, D, E, F, G, H)> for (ExA, ExB, ExC, ExD, ExE, ExF, ExG, ExH) where ExA: Default + Extend<A>, ExB: Default + Extend<B>, ExC: Default + Extend<C>, ExD: Default + Extend<D>, ExE: Default + Extend<E>, ExF: Default + Extend<F>, ExG: Default + Extend<G>, ExH: Default + Extend<H>,impl<A, B, C, D, E, F, G, H, I, ExA, ExB, ExC, ExD, ExE, ExF, ExG, ExH, ExI> FromIterator<(A, B, C, D, E, F, G, H, I)> for (ExA, ExB, ExC, ExD, ExE, ExF, ExG, ExH, ExI) where ExA: Default + Extend<A>, ExB: Default + Extend<B>, ExC: Default + Extend<C>, ExD: Default + Extend<D>, ExE: Default + Extend<E>, ExF: Default + Extend<F>, ExG: Default + Extend<G>, ExH: Default + Extend<H>, ExI: Default + Extend<I>,impl<A, B, C, D, E, F, G, H, I, J, ExA, ExB, ExC, ExD, ExE, ExF, ExG, ExH, ExI, ExJ> FromIterator<(A, B, C, D, E, F, G, H, I, J)> for (ExA, ExB, ExC, ExD, ExE, ExF, ExG, ExH, ExI, ExJ) where ExA: Default + Extend<A>, ExB: Default + Extend<B>, ExC: Default + Extend<C>, ExD: Default + Extend<D>, ExE: Default + Extend<E>, ExF: Default + Extend<F>, ExG: Default + Extend<G>, ExH: Default + Extend<H>, ExI: Default + Extend<I>, ExJ: Default + Extend<J>,impl<A, B, C, D, E, F, G, H, I, J, K, ExA, ExB, ExC, ExD, ExE, ExF, ExG, ExH, ExI, ExJ, ExK> FromIterator<(A, B, C, D, E, F, G, H, I, J, K)> for (ExA, ExB, ExC, ExD, ExE, ExF, ExG, ExH, ExI, ExJ, ExK) where ExA: Default + Extend<A>, ExB: Default + Extend<B>, ExC: Default + Extend<C>, ExD: Default + Extend<D>, ExE: Default + Extend<E>, ExF: Default + Extend<F>, ExG: Default + Extend<G>, ExH: Default + Extend<H>, ExI: Default + Extend<I>, ExJ: Default + Extend<J>, ExK: Default + Extend<K>,impl<A, B, C, D, E, F, G, H, I, J, K, L, ExA, ExB, ExC, ExD, ExE, ExF, ExG, ExH, ExI, ExJ, ExK, ExL> FromIterator<(A, B, C, D, E, F, G, H, I, J, K, L)> for (ExA, ExB, ExC, ExD, ExE, ExF, ExG, ExH, ExI, ExJ, ExK, ExL) where ExA: Default + Extend<A>, ExB: Default + Extend<B>, ExC: Default + Extend<C>, ExD: Default + Extend<D>, ExE: Default + Extend<E>, ExF: Default + Extend<F>, ExG: Default + Extend<G>, ExH: Default + Extend<H>, ExI: Default + Extend<I>, ExJ: Default + Extend<J>, ExK: Default + Extend<K>, ExL: Default + Extend<L>,impl<A, B, C, D, ExA, ExB, ExC, ExD> FromIterator<(A, B, C, D)> for (ExA, ExB, ExC, ExD) where ExA: Default + Extend<A>, ExB: Default + Extend<B>, ExC: Default + Extend<C>, ExD: Default + Extend<D>,impl<A, B, C, ExA, ExB, ExC> FromIterator<(A, B, C)> for (ExA, ExB, ExC) where ExA: Default + Extend<A>, ExB: Default + Extend<B>, ExC: Default + Extend<C>,impl<A, B, ExA, ExB> FromIterator<(A, B)> for (ExA, ExB) where ExA: Default + Extend<A>, ExB: Default + Extend<B>,impl<A, E, V: FromIterator<A>> FromIterator<Result<A, E>> for Result<V, E>impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V>impl<T, ExtendT> FromIterator<(T,)> for (ExtendT,) where ExtendT: Default + Extend<T>,