-
Book Overview & Buying
-
Table Of Contents
The Rust Programming Handbook
By :
The real power of iterators comes from iterator adapters. These are methods defined on the Iterator trait that take an iterator and return a new, transformed iterator without consuming the original one immediately. Because adapters return new iterators, you can chain multiple adapters together to perform complex operations in a readable way. Since iterators are lazy, no actual computation happens until you call a consuming method (such as collect() or sum()) at the end of the chain.
One of the most popular and handy adapters you might come across is .map(). It’s a really useful tool for transforming data: it takes an iterator and creates a new one by applying a simple function to each item, turning each into something new. It’s great for converting data types, doing calculations on every number in a sequence, or changing every string in a list.
The way you close the .map() function really shows how...