Trait FromIterator
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!;