liveBook · Manning

Chapter 8: Iterators and closures

What you will learn in chapter 8 of Learn Rust in a Month of Lunches

The chapter introduces method chaining as passing one method’s output into the next and identifies iterators and closures as tools used with that style.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces method chaining as passing one method’s output into the next and identifies iterators and closures as tools used with that style.

Key terms: method chaining, output, input, iterators, collections, closures, scope, capture variables

8.1 Chaining methods

The section contrasts imperative and functional Rust code and shows how chaining methods can build a Vec.

Key terms: imperative style, functional style, expressions, range, iterator, .take(), .collect(), Vec, chain

8.2 Iterators

This section explains how iterators yield items, how different iterator methods affect a collection, and how to implement an iterator for a custom type.

Key terms: iterator, .next(), Option, Some, None, .iter(), .iter_mut(), .into_iter(), references, mutable references

8.3 Closures and closures inside iterators

This section introduces closures as callable, unnamed functions and explains how they can use variables from their surrounding environment.

Key terms: closure, anonymous function, ||, code block, environment, encloses

8.3.1 Closures inside of methods

This section shows how closures customize method behavior, how a closure can provide a fallback value, and how to add indexes to iterator items.

Key terms: closure, .for_each(), signature, .unwrap_or(), .unwrap_or_else(), default value, .enumerate(), .map(), index, iterator

8.3.2 Closures: Lazy and fast

This section explains why iterator chains do no work until consumed, how that supports fast code, and how to collect paired iterators into a HashMap.

Key terms: iterator, .map(), .collect(), lazy, consume, zero-cost abstractions, run time, HashMap, .into_iter(), .zip()

8.3.3 |_| in a closure

Explains that `|_|` lets a closure accept a required argument without using it.

Key terms: closure, |_|, ||, argument

8.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 7: Traits: Making different types do the same thing Chapter 9: Iterators and closures again! →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.