Iterators
In Rust, iterators provide a way to traverse and operate on collections of data. They allow you to perform operations like filtering, mapping, and folding over elements without explicitly writing loops. Here's an example that demonstrates the usage of iterators in Rust:
fn main() { let numbers: Vec<i32> = vec![1, 2, 3, 4, 5]; // Creating an iterator from the vector let iter = numbers.iter(); // Iterating over the elements and printing them for num in iter { println!("Number: {}", num); } // Using iterator methods: map and filter let squares: Vec<i32> = numbers.iter() .map(|&x| x * x) .collect(); println!("Squares: {:?}", squares); let even_numbers: Vec<&i32> = numbers.iter() .filter(|&x| x % 2 == 0) .collect(); println!("Even Numbers: {:?}", even_numbers); // Chaining multiple iterator methods let sum: i32 = numbers.iter() .filter(|&x| x % 2 != 0) .map(|&x| x * x) .sum(); println!("Sum of squares of odd numbers: {}", sum); }
In this example, we start by creating a vector of numbers. We then create an iterator iter
from the vector using the iter()
method.
We iterate over the elements of the vector using a for
loop and print each number.
Next, we demonstrate the use of iterator methods. We use the map()
method to transform each element into its square and collect the results into a new vector called squares
. Similarly, we use the filter()
method to keep only the even numbers and collect them into a new vector called even_numbers
.
Lastly, we chain multiple iterator methods together. We filter out the odd numbers, square each of them, and compute their sum using the sum()
method.
The Rust standard library provides a rich set of iterator methods that you can use to perform various operations on collections, such as map()
, filter()
, fold()
, sum()
, collect()
, and many more. Iterators allow you to write concise and expressive code when working with collections in Rust.
Here is an example of how to use iterators in Rust:
fn main() { let numbers = [1, 2, 3, 4, 5]; let mut iterator = numbers.iter(); while let Some(number) = iterator.next() { println!("{}", number); } }
This code defines a function called main()
that iterates over the numbers
array using an iterator. The iterator is created by calling the iter()
method on the numbers
array. The iter()
method returns an iterator that can be used to iterate over the elements of the array.
The while let
loop iterates over the iterator, calling the next()
method on the iterator to get the next element. The next()
method returns an Option<T>
, which is a type that can either contain a value of type T
or the None
value. If the next()
method returns None
, the loop is finished.
In this example, the next()
method will return a value of type i32
for each element in the numbers
array. The value of the element is then printed to the console.
Here is a breakdown of the code:
- The
numbers
array is a collection of five integers. - The
iterator
variable is an iterator that can be used to iterate over the elements of thenumbers
array. - The
while let
loop iterates over the iterator, calling thenext()
method on the iterator to get the next element. - The
println!()
macro prints the value of the element to the console.