Iterators are a powerful feature in Rust that allow you to process sequences of elements efficiently. They provide a way to traverse collections and perform operations on their items without exposing the underlying implementation details.
In Rust, an iterator is an object that implements the Iterator
trait. This trait defines a method called next()
, which returns the next item in the sequence. Iterators are lazy, meaning they only compute values when requested, which can lead to improved performance and memory usage.
Many Rust collections provide methods to create iterators. For example:
let numbers = vec![1, 2, 3, 4, 5];
let iter = numbers.iter();
You can also create iterators using the iter()
method on arrays or slices:
let array = [10, 20, 30];
let iter = array.iter();
Rust provides numerous methods for working with iterators. Here are some of the most commonly used ones:
map()
: Transform each elementfilter()
: Keep elements that satisfy a predicatecollect()
: Gather the iterator's elements into a collectionfold()
: Reduce the iterator to a single valueenumerate()
: Yield the current count and the element during iteration
let numbers = vec![1, 2, 3, 4, 5];
let sum: i32 = numbers.iter()
.filter(|&x| x % 2 == 0)
.map(|&x| x * x)
.sum();
println!("Sum of squares of even numbers: {}", sum);
In this example, we filter even numbers, square them, and then sum the results.
Rust's for
loop works seamlessly with iterators. When you use a for
loop, Rust automatically calls into_iter()
on the collection:
let words = vec!["Hello", "World", "Rust"];
for word in words {
println!("{}", word);
}
You can create your own iterators by implementing the Iterator
trait for your types. This involves defining the next()
method:
struct CountDown(u32);
impl Iterator for CountDown {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
if self.0 == 0 {
None
} else {
let current = self.0;
self.0 -= 1;
Some(current)
}
}
}
let countdown = CountDown(5);
for num in countdown {
println!("{}", num);
}
iter()
for shared references, iter_mut()
for mutable references, and into_iter()
when you need to consume the collection.To deepen your understanding of Rust iterators, explore these related topics:
Mastering iterators is crucial for writing efficient and expressive Rust code. They form the backbone of many high-performance operations and are an essential tool in every Rust programmer's toolkit.