9 Iterators and closures again!

 

This chapter covers

  • Using filtering to keep what you want in an iterator
  • Reversing, zipping, and cycling iterators
  • Taking just the values you want in an iterator
  • Determining whether anything or everything in an iterator matches a condition
  • Many other methods too numerous to mention here but essential to know
  • Using the dbg! macro to see what your code is doing at any point

Iterators and closures in Rust have so many methods that we need another full chapter to go over them. There are a lot of these methods, but it’s worth the effort to learn them because they do a lot of work for you. You might not memorize them all during your first reading, but if you remember their names and what they do, you can look them up later when you need them.

9.1 Helpful methods for closures and iterators

Rust becomes an even more fun language once you become comfortable with closures. As we saw in the last chapter, with closures you can “chain” methods to each other and do a lot of things with very little code. And the more of them you know, the more you can chain together. This chapter is mostly going to show you how to use certain common iterator methods that work conveniently with closures.

9.1.1 Mapping and filtering

9.1.2 Some more iterator and related methods

9.1.3 Checking and finding items inside iterators

9.1.4 Cycling, zipping, folding, and more

9.2 The dbg! macro and .inspect

Summary