Skip to content
← All Rust tracks

Closures and Iterators

Rust

Use closures and iterator adapters to transform data. Explore captures, the Fn traits, lazy evaluation and custom iterator implementations.

This track uses Rust. You can make it your practice mode to browse related content across the site.

25 exercises

3 articles

Explore freely. Sign in to keep your progress.

Your learning sequence

  1. 1 Not solved yet. Closures 101: build a pipeline of boxed closures Practice · Easy
  2. 2 Not solved yet. Fn, FnMut, FnOnce: the closure hierarchy Practice · Medium
  3. 3 Not solved yet. move: when the closure must own its captures Practice · Medium
  4. 4 Disjoint field capture: what changed in edition 2021 Read
  5. 5 Not solved yet. iter, iter_mut, into_iter: all three, one Vec Practice · Easy
  6. 6 Not solved yet. Sum collections and iterators with IntoIterator Practice · Medium
  7. 7 Not solved yet. Filter borrowed values into owned results Practice · Medium
  8. 8 Not solved yet. Trace a lazy map, filter and take pipeline Practice · Easy
  9. 9 Not solved yet. map, filter, filter_map: parse host:port lines Practice · Easy
  10. 10 Not solved yet. fold and try_fold: a total that refuses to overflow Practice · Medium
  11. 11 Not solved yet. Split a message at its first blank line Practice · Easy
  12. 12 Not solved yet. zip and enumerate: pair two lists and report the leftovers Practice · Easy
  13. 13 Not solved yet. peekable and next_if: write a tokeniser Practice · Hard
  14. 14 Not solved yet. flat_map: expand a range spec like "1-3,7,10-12" Practice · Medium
  15. 15 Not solved yet. scan: a running balance that stops at the floor Practice · Medium
  16. 16 Not solved yet. Implement Iterator by hand: the Collatz sequence Practice · Medium
  17. 17 Not solved yet. from_fn and successors: iterators without writing a struct Practice · Easy
  18. 18 Not solved yet. any, all, find, position: audit a list of records Practice · Easy
  19. 19 Not solved yet. Choose the first minimum and last maximum Practice · Medium
  20. 20 Not solved yet. partition and unzip: split rows and group names Practice · Medium
  21. 21 Not solved yet. collect and the turbofish: three targets, one source Practice · Medium
  22. 22 Not solved yet. size_hint and DoubleEndedIterator: an iterator with two ends Practice · Hard
  23. 23 Not solved yet. Write your own adapters, with an extension trait Practice · Hard
  24. 24 Not solved yet. Make your own collection iterable, all three ways Practice · Hard
  25. 25 When collect() is a mistake Read
  26. 26 Not solved yet. Chains versus loops: top-k, and a function that must not allocate Practice · Medium
  27. 27 Not solved yet. Read a log after its closure finishes Practice · Medium
  28. 28 How iterator consumers shape a loop Read

Check yourself

4 questions · not saved while you're signed out

Use these questions to check your understanding and compare explanations. Your score does not affect track completion.

0 / 4 correct

Why can this move closure be called twice even though s has moved into it?

Choose one answer to reveal the explanations. Answers are kept only while this page is open.

fn main() {
    let s = String::from("owned");
    let length = move || s.len();
    println!("{} {}", length(), length());
}
Question 1 of 4