Iterators and closures in Rust
Learn Rust · Beginner · 12 min · After Lifetimes in Rust: how long a reference lives
How do you transform a collection without writing the loop yourself?
1Guess
fn main() { let v = vec![1, 2, 3, 4]; let s: i32 = v.iter().filter(|x| **x % 2 == 0).map(|x| x * x).sum(); println!("{s}"); }2Read
Closures
A closure is an anonymous function you can store in a variable or pass around:
|x| x + n. Unlike a plain function it can capture variables from where it is written. By default it borrows them. Putmovebefore the bars and it takes ownership instead, which you need when the closure outlives the variables. A parameter of typeimpl Fn(i32) -> i32accepts a closure.3Code
Capturing by reference and by move
1 / 2fn apply(f: impl Fn(i32) -> i32, value: i32) -> i32 { f(value) } fn main() { let n = 10; let add_n = |x| x + n; println!("{}", add_n(5)); println!("{}", apply(add_n, 1)); let name = String::from("Ferris"); let greet = move || format!("Hello, {name}!"); println!("{}", greet()); }Read the program. What will it print?
4Read
Lazy iterators
An iterator produces items one at a time. It is lazy:
map,filter,enumerateandzipare adapters that only describe a chain. Nothing runs until a consumer such ascollect,sum,countor aforloop pulls the items.iter()borrows the items, andinto_iter()takes ownership of them.5Code
Iterator chains
1 / 2fn main() { let sentence = "the quick brown fox jumped"; let lengths = sentence .split_whitespace() .map(|w| w.len()) .collect::<Vec<_>>(); println!("{lengths:?}"); for (i, word) in sentence.split_whitespace().enumerate() { println!("{i}: {word}"); } let total = lengths.iter().fold(0, |acc, n| acc + n); println!("total {total}"); let longest = sentence.split_whitespace().max_by_key(|w| w.len()); println!("{longest:?}"); }Read the program. What will it print?
6Fix
fn main() { let v = vec![1, 2, 3]; for x in v { println!("{x}"); } println!("{} numbers", v.len()); }7Say it back
8Mission · 10 min
From a list of words, using only iterator chains, print how many there are, the longest word and the words longer than 4 letters in upper case.
cargo new word-stats cd word-stats cargo run
Common questions
- What is a closure in Rust?
- A closure is an anonymous function written with bars, like |x| x + 1. It can capture variables from the scope around it, by reference by default or by ownership with the move keyword. You can store it in a variable or pass it to a function that takes impl Fn, for example to transform each item of a collection.
- What does lazy mean for Rust iterators?
- Iterator adapters such as map and filter do no work when you call them. They only build a new iterator. The items are produced when a consumer like collect, sum, count or a for loop asks for them. So you can chain many steps and the data is walked once, item by item.
- What is the difference between iter and into_iter?
- iter() borrows the collection and gives references to its items, so the collection stays usable afterwards. into_iter() takes ownership and gives the items themselves, so the collection is moved and cannot be used again. A for loop over a vector uses into_iter, and a for loop over &vector uses iter.
Sources
- https://doc.rust-lang.org/book/ch13-01-closures.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch13-02-iterators.html (read 2026-12-03)
- https://doc.rust-lang.org/std/iter/trait.Iterator.html (read 2026-12-03)
Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03
Published Dec 3, 2026 · Updated Dec 3, 2026