src/iterators/collect.md
collectThe collect method lets you build a collection from an Iterator.
# // Copyright 2024 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
fn main() {
let primes = vec![2, 3, 5, 7];
let prime_squares = primes.into_iter().map(|p| p * p).collect::<Vec<_>>();
println!("prime_squares: {prime_squares:?}");
}
Vec, VecDeque, or HashSet.
Iterators that produce key-value pairs (i.e. a two-element tuple) can also be
collected into HashMap and BTreeMap.Show the students the definition for collect in the standard library docs.
There are two ways to specify the generic type B for this method:
some_iterator.collect::<COLLECTION_TYPE>(), as shown.
The _ shorthand used here lets Rust infer the type of the Vec elements.let prime_squares: Vec<_> = some_iterator.collect().
Rewrite the example to use this form.FromIterator trait, which defines how each type of collection gets
built from an iterator.FromIterator for Vec,
HashMap, etc., there are also more specialized implementations which let you
do cool things like convert an Iterator<Item = Result<V, E>> into a
Result<Vec<V>, E>.collect is because it's
generic over its return type. This makes it harder for the compiler to infer
the correct type in many cases.