Leanpub Header

Skip to main content

The Rust Iterator Cookbook

78 recipes for writing expressive, efficient, and idiomatic iterator pipelines in Rust.

Every .map(), .filter(), and .collect() you write is built on a single method: next(). This book shows you what's underneath — and what you can build on top.

$29.99

With Coupon

$10.00

You pay

$10.00

Author earns

$8.00
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
164
Pages
27,197Words
About

About

About the Book

The Rust Iterators Cookbook teaches you to write expressive, efficient iterator pipelines through 78 practical recipes. Each one starts with a real problem, shows working Rust code, and explains the why — not just the what.

Covers everything from lazy evaluation and custom adaptors to parallel iteration with Rayon, async streams, error handling in pipelines, and the itertools crate. Includes 17 hands-on exercises with Rust Playground solutions.

For Rust developers who already know the language and want to master one of its most powerful features.

Share this book

Author

About the Author

Eugenio Perrotta Neto

Eugenio Perrotta Neto is a Senior Backend Engineer with 19 years of experience building high-performance systems. He currently works on low-latency trading infrastructure in Rust at a Brazilian market-making firm integrated with the B3 exchange. He writes about systems programming, Rust, and the lessons learned building production software at scale.

Contents

Table of Contents

Rust Iterators Cookbook

  1. Preface

Chapter 1 — How Iterators Work

  1. The concept
  2. Recipe 1 — Iterating over a Vec without cloning
  3. Recipe 2 — Building an infinite iterator with next()
  4. Recipe 3 — Understanding why .map() alone does nothing

Chapter 2 — Choosing the Right Iterator

  1. The concept
  2. Recipe 4 — Reading a collection without consuming it
  3. Recipe 5 — Modifying elements in place
  4. Recipe 6 — Consuming a collection to produce a new one
  5. Recipe 7 — Understanding what for x in v actually does

Chapter 3 — Lazy Evaluation

  1. The concept
  2. Recipe 8 — Confirming that adaptors are lazy
  3. Recipe 9 — Short-circuiting an expensive pipeline with take()
  4. Recipe 10 — Avoiding work with find() and position()
  5. Recipe 11 — Comparing lazy chains vs eager loops for clarity

Chapter 4 — Transforming Data

  1. The concept
  2. Recipe 12 — Transforming every element with map()
  3. Recipe 13 — Flattening nested collections with flat_map()
  4. Recipe 14 — Collapsing nested iterators with flatten()
  5. Recipe 15 — Pairing elements from two iterators with zip()
  6. Recipe 16 — Attaching an index with enumerate()
  7. Recipe 17 — Concatenating iterators with chain()

Chapter 5 — Filtering and Selecting

  1. The concept
  2. Recipe 18 — Keeping elements that match a condition with filter()
  3. Recipe 19 — Filtering and transforming in one step with filter_map()
  4. Recipe 20 — Taking the first N elements with take() and take_while()
  5. Recipe 21 — Skipping elements with skip() and skip_while()
  6. Recipe 22 — Sampling every Nth element with step_by()

Chapter 6 — Aggregating and Consuming

  1. The concept
  2. Recipe 23 — Collecting into different container types
  3. Recipe 24 — Reducing to a single value with fold()
  4. Recipe 25 — Using reduce() when no initial value is needed
  5. Recipe 26 — Summing and counting
  6. Recipe 27 — Short-circuiting checks with any() and all()
  7. Recipe 28 — Consuming for side effects with for_each()

Chapter 7 — Composing Pipelines

  1. The concept
  2. Recipe 29 — Building a log processing pipeline
  3. Recipe 30 — Parsing a CSV with a fallible pipeline
  4. Recipe 31 — Deduplicating and normalizing a sequence
  5. Recipe 32 — Grouping elements with fold() and a HashMap
  6. Recipe 33 — Flattening a pipeline with nested Options and Results
  7. Recipe 34 — Knowing when not to use a pipeline

Chapter 8 — Implementing Iterator from Scratch

  1. The concept
  2. Recipe 35 — Iterating over fields of a struct
  3. Recipe 36 — An iterator with internal state
  4. Recipe 37 — A borrowing iterator with a lifetime
  5. Recipe 38 — Iterating over a recursive data structure
  6. Recipe 39 — Implementing size_hint() for better collect() performance

Chapter 9 — Creating Custom Adaptors

  1. The concept
  2. Recipe 40 — A simple stateless adaptor
  3. Recipe 41 — A stateful adaptor with an accumulator
  4. Recipe 42 — An adaptor that transforms and filters simultaneously
  5. Recipe 43 — A generic adaptor with full type flexibility

Chapter 10 — Specialized Iterator Traits

  1. The concept
  2. Recipe 44 — Iterating in reverse with DoubleEndedIterator
  3. Recipe 45 — Implementing DoubleEndedIterator for a custom type
  4. Recipe 46 — Peeking at the next element without consuming it
  5. Recipe 47 — Implementing FusedIterator for correctness
  6. Recipe 48 — Using size_hint() and len() downstream

Chapter 11 — Performance and Zero-Cost Abstractions

  1. The concept
  2. Recipe 49 — Verifying zero-cost with a benchmark
  3. Recipe 50 — Avoiding intermediate allocations
  4. Recipe 51 — Choosing between iterator chains and for loops for performance
  5. Recipe 52 — When collect() is expensive and what to do instead
  6. Recipe 53 — Exploiting SIMD via iterator chains
  7. Recipe 54 — Profiling an iterator pipeline

Chapter 12 — Parallel Iterators with Rayon

  1. The concept
  2. Recipe 55 — Drop-in parallelism with par_iter()
  3. Recipe 56 — Parallel aggregation with reduce() and sum()
  4. Recipe 57 — Parallel flat_map and collecting into a HashMap
  5. Recipe 58 — Knowing when not to use Rayon

Chapter 13 — Async Iterators with Stream

  1. The concept
  2. Recipe 59 — Creating a Stream from a Vec and consuming it
  3. Recipe 60 — Streaming lines from a file
  4. Recipe 61 — Consuming a channel as a Stream
  5. Recipe 62 — Transforming a Stream with map() and then()
  6. Recipe 63 — Merging multiple Streams
  7. Recipe 64 — Stream vs Iterator: when to use which

Chapter 14 — Error Handling in Pipelines

  1. The concept
  2. Recipe 65 — Fail fast: stop at the first error
  3. Recipe 66 — Collect all errors before reporting
  4. Recipe 67 — Skip errors silently
  5. Recipe 68 — Using the ? operator inside closures
  6. Recipe 69 — Transforming error types mid-pipeline
  7. Recipe 70 — Building a pipeline that returns Result

Chapter 17 — The itertools Crate

  1. The concept
  2. Recipe 71 — Grouping consecutive elements with chunk_by()
  3. Recipe 72 — Sliding windows with tuple_windows()
  4. Recipe 73 — Cartesian product with cartesian_product()
  5. Recipe 74 — Combinations and permutations
  6. Recipe 75 — Interleaving and merging sequences
  7. Recipe 76 — Collecting into multiple collections at once with partition_map()
  8. Recipe 77 — Unique elements with unique() and unique_by()
  9. Recipe 78 — Formatting with join() and format_with()
  10. When to reach for itertools

Chapter 15 — Exercises

  1. Chapter 1 — How Iterators Work
  2. Chapter 2 — Choosing the Right Iterator
  3. Chapter 3 — Lazy Evaluation
  4. Chapter 4 — Transforming Data
  5. Chapter 5 — Filtering and Selecting
  6. Chapter 6 — Aggregating and Consuming
  7. Chapter 7 — Composing Pipelines
  8. Chapter 8 — Implementing Iterator
  9. Chapter 9 — Custom Adaptors
  10. Chapter 14 — Error Handling in Pipelines
  11. Chapter 11 — Performance

Chapter 16 — When Not to Use Iterators

  1. When the control flow has multiple exit conditions
  2. When two cursors move independently through the same sequence
  3. When you need to mutate while inspecting neighbors
  4. When the pipeline becomes unreadable
  5. When you need random access mid-iteration
  6. When you are debugging
  7. Summary

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub