Slicing and iteration
Similar to how interfaces standardize access to functionality in the libraries of other languages, Rust's standard library utilizes a type and a trait to provide fundamental implementations. The trait, Iterator<T>
, has been looked at and used over the course of this book several times. The slice type, however, was not explicitly used a lot, especially since the Rust compiler automatically uses slices when Vec<T>
is borrowed for a function call. How can you leverage this type, though? We have seen the Iterator<T>
implementation in action, but does it provide more than that?
Iterator
To recap: an iterator is a pattern to traverse a collection, providing a pointer to each element in the process. This pattern is mentioned in the book Design Patterns, by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (the Gang of Four), in 1994 and can be found in basically every language one way or another.
In Rust, the term pointer to each element gets a...