-
Book Overview & Buying
-
Table Of Contents
Design Patterns and Best Practices in Rust
By :
The Iterator pattern provides a way to access elements of a collection sequentially without exposing the underlying representation. Unlike in many other languages where iterators are implemented as separate design patterns, in Rust they're a core feature of the language, deeply integrated into the standard library and language syntax. This tight integration makes iterators both powerful and efficient.
In Rust, the Iterator pattern is formalized through the Iterator trait:
pub trait Iterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
// Many default methods provided...
}
This trait has a single required method, next(), which returns the next element of the collection, or None if there are no more elements. The trait also provides many default methods, such as map (which transforms each element), filter (which selects elements matching a predicate), fold (which accumulates elements...