Home
Map
collect ExamplesLearn about the collect function and how it turns an iterator into a usable collection like a Vec or String.
Rust
This page was last reviewed on Feb 7, 2023.
Collect. In Rust programs, iterators are often used to modify or generate data. But to have a more usable collection, like a Vec or String, we must convert the iterator.
With collect, we have a powerful built-in function that converts an iterator into a type like a Vec or String. Collect can perform many conversions.
vec
iter
into iter
String example. Suppose we have an iterator of char values—for example, by having a Vec of chars that we call iter() upon. We can collect() these into a String.
Here We use the turbofish operator to collect the iterator of characters into a String.
fn main() { let values: Vec<char> = vec!['a', 'b', 'c']; // Convert a vector of chars into a string. let result = values.iter().collect::<String>(); println!("{result}"); }
abc
Map example. The map() function can be used to transform the elements in an iterator with a function argument. Here we uppercase all the strings in a iterator of strings (created by iter).
map
Then We call the collect() function to convert the result of map into a Vector of Strings.
String Array
fn main() { let mut values = Vec::new(); values.push("bird"); values.push("cat"); // Map a vector of strings into uppercase strings. // Then collect the result of map into a vector of strings. let result = values.iter().map(|a| a.to_uppercase()).collect::<Vec<String>>(); println!("{:?}", result); }
["BIRD", "CAT"]
Enumerate. In Rust we often use the enumerate() function to add indexes to an iterator. Each returned element has an index and the original value in a tuple.
Tip We can invoke collect() on the result of enumerate() to transform a vector into a vector of 2-element tuples.
Tuple
usize
fn main() { let values = vec!["abc".to_string(), "def".to_string()]; // Convert string vector into a vector of tuples of index, String pairs. let result = values.iter().enumerate().collect::<Vec<(usize, &String)>>(); println!("{:?}", result); }
[(0, "abc"), (1, "def")]
Turbofish syntax. Rust supports the turbofish syntax, which is a way to cast the result of a function in its calling location. Here we examine this syntax form.
Version 1 This version of the code uses the turbofish operator on collect() to cast the result of the rev() function call.
Version 2 Here we use collect() in the same way, but avoid turbofish and specify the type of the "result2" local variable.
fn main() { let numbers = vec![10, 20, 30]; // Version 1: use collect with turbofish. let result = numbers.iter().rev().collect::<Vec<&usize>>(); println!("{:?}", result); // Version 2: use collect with type annotation. let result2: Vec<&usize> = numbers.iter().rev().collect(); println!("{:?}", result2); }
[30, 20, 10] [30, 20, 10]
Collect optimization. How can we use collect() to optimize Rust programs? With collect, we can store the results of an iterator like bytes() in a vector.
Version 1 This version of the code loops over the bytes of the string directly, and sums the values of the bytes.
for
Version 2 This version loops over a vector that was created with collect() from the bytes of the string. It also sums the bytes.
Result It is much faster to loop over a vector of u8 values created by calling collect.
use std::time::*; fn main() { let source = "bird is blue"; let v = source.bytes().collect::<Vec<u8>>(); // Version 1: loop over bytes iterator. let t0 = Instant::now(); let mut sum0 = 0; for _ in 0..1000000000 { for b in source.bytes() { sum0 += b as usize; } } println!("{:?}", t0.elapsed()); // Version 2: loop over bytes vector. let t1 = Instant::now(); let mut sum1 = 0; for _ in 0..1000000000 { for &b in &v { sum1 += b as usize; } } println!("{:?}", t1.elapsed()); println!("{}", sum0); println!("{}", sum1); }
4.413035666s bytes() 156.269833ms &v 1125000000000 1125000000000
Split. When calling the split() function on a string in Rust, we often want to collect() the results. But if we can avoid doing this, often the code will run faster.
String split
A summary. Collect() is a powerful and general-purpose conversion function for iterators. It is used at the end of a chain of iterator function calls to convert the result into a more usable type.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 7, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.