liveBook · Manning

Lazy evaluation

As explained in Idiomatic Rust

Lazy evaluation is an execution strategy that postpones computing an expression until its result is required.

Lazy evaluation postpones computing an expression until its result is needed. In Rust, iterator methods such as map() can return another iterator without computing the transformed values; collect() forces evaluation. section 3.2.5

let arr = [1, 2, 3, 4];
println!("{:?}", arr);
let vec: Vec<_> = arr.iter().map(|v| v.to_string()).collect();
println!("{:?}", vec);

This iterator maps each array value to a string; collect() evaluates the transformation and builds a vector. section 3.2.5

How Idiomatic Rust covers it

How deep it goes
Explains that map returns another iterator and collect forces evaluation, without detailing iterator internals.
Examples
Demonstrates converting array integers to strings with map and collect.
What you can do after
Apply lazy transformations with Rust iterator methods such as map, then force results with collect.
How it connects
Connects lazy evaluation to iterators, map, and collect.

Also explained in

Compare how 13 books explain lazy evaluation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Idiomatic Rust. The section links point to the parts of the book it draws on.