Rust Iterators: Navigate and Manipulate Collections Efficiently
[tr] Türkçe Oku 2024-04-15
In the Rust programming language, iterators are structures used to traverse and perform operations on collections. They provide a way to access elements of data collections and manipulate them while adhering to Rust’s strict ownership and borrowing rules, ensuring safe and efficient usage.
Defining and Using Iterators
The basic way to utilize an iterator is through the .iter()
, .iter_mut()
, and .into_iter()
methods, which differ based on the ownership status of the collection:
.iter()
: Iterates over immutable references of the collection, allowing you to access elements without modifying the original collection..iter_mut()
: Iterates over mutable references, enabling you to modify the elements of the collection..into_iter()
: Takes ownership of the collection to iterate, consuming the collection and allowing operations that take ownership of the elements.
Simple Example
Let’s start with iterating over a vector to print its elements:
let numbers = vec![1, 2, 3, 4, 5];
// Using .iter() for immutable references
for num in numbers.iter() {
println!("{}", num);
}
Making Modifications
You can modify elements with mutable references:
let mut numbers = vec![1, 2, 3, 4, 5];
for num in numbers.iter_mut() {
*num += 1; // Add 1 to each element
}
println!("{:?}", numbers); // Output: [2, 3, 4, 5, 6]
Iteration with Ownership
Iterate over a collection by taking ownership:
let numbers = vec![1, 2, 3, 4, 5];
let doubled: Vec<_> = numbers.into_iter().map(|num| num * 2).collect();
println!("{:?}", doubled); // Output: [2, 4, 6, 8, 10]
Common Iterator Methods
Rust iterators come with many useful methods, some of which include:
.map()
: Transforms elements using a specified function..filter()
: Filters elements based on a condition..fold()
: Reduces all elements into a single value starting from an initial value.
Example: Combining map
and filter
Let’s select only the even numbers from a list and double each:
let numbers = vec![1, 2, 3, 4, 5];
let processed: Vec<_> = numbers.iter()
.filter(|&&x| x % 2 == 0)
.map(|&x| x * 2)
.collect();
println!("{:?}", processed); // Output: [4, 8]
Learning and practicing with iterators is key to effectively using Rust. Rust’s documentation and community resources are excellent sources for gaining deeper knowledge on this topic.