Applied Intermediate

12  Iterators

Iterator chains replace most hand-written loops. Build a value by chaining adaptors and finishing with a consumer like sum or collect.

Step 2 / 3

In long_word_lengths, collect the lengths of the words longer than 3 characters into a Vec.

Hints
  • Finish a chain with .collect(); the return type tells it to build a Vec.
12_iterators.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
fn long_word_lengths(words: &[&str]) -> Vec<usize> {
    words.iter().filter(|w| w.len() > 3).map(|w| w.len()).collect()
}