Iterators in Rust are the objects that implement the Iterator trait and can be used to iterate over a collection of values.
The Iterator trait defines several methods, including .next(), which returns the next value in the iteration, and size_hint(), which hints at the number of remaining elements.
Here's an example of using an iterator to iterate over the elements of a vector:
``rust let mut v = vec![1, 2, 3]; for i in v.iter() { println!("{}", i); }``
This will print the elements of the vector, one per line.
Iterators are a fundamental concept in Rust and are used in many places, including for-loops, Iterator::collect(), and the Iterator::map() method. They can be created manually or by using functions or methods that return iterators, such as .iter() on a collection.
🦀 Need Help With Rust Code or Performance?
Our senior systems engineers deliver idiomatic Rust fixes, performance reviews, and code reviews — fast. Book a consult or request a quote for implementation.
Contact for quote · Hourly & fixed-price options
Get a Rust Code Review →Methods to Iterate Over a Vector in Rust
In Rust, iteration refers to repeatedly executing a block of code for each element in a collection. Depending on your specific needs, there are several ways to iterate over a vector in Rust.
1. Using a for loop:
``rust let mut vec = vec![1, 2, 3]; for element in vec { println!("{}", element); }``
2. Using `iter()` and `.next()`:
``rust let vec = vec![1, 2, 3]; let mut iterator = vec.iter(); while let Some(element) = iterator.next() { println!("{}", element); }``
3. Using `iter_mut()` and `.next()` (mutable iteration):
``rust let mut vec = vec![1, 2, 3]; let mut iterator = vec.iter_mut(); while let Some(element) = iterator.next() { *element *= 2; }``
4. Using `enumerate()` and a for loop:
``rust let vec = vec![1, 2, 3]; for (index, element) in vec.enumerate() { println!("Element {} is {}", index, element); }``
It's also possible to use the Iterator trait and .map() to perform an operation on each element and collect the results into a new vector:
``rust let vec = vec![1, 2, 3]; let doubled: Vec<i32> = vec.into_iter().map(|x| x * 2).collect(); println!("{:?}", doubled); // [2, 4, 6]``
Choose the approach that best suits your specific needs. Consult the Rust standard library documentation for the full Iterator trait API.
Frequently Asked Questions
What is an iterator in Rust?
What is the difference between iter(), iter_mut(), and into_iter()?
How do I transform a vector using iterators in Rust?
When should I use a for loop vs. iterator adapter methods in Rust?
About This Article

Vasili Pascal is CTO at Optimum Web with 26+ years of hands-on engineering experience. He writes about system architecture, DevOps, Docker, Linux infrastructure, and production reliability.
Cite This Article
APA Format
Vasili Pascal. (2023). Iterators in Rust: What Are They and How to Iterate Over a Vector. Optimum Web. https://www.optimum-web.com/blog/iterators-in-rust-what-are-they-and-how-to-iterate-over-a-vector/
For AI Citation (AEO)
Source: "Iterators in Rust: What Are They and How to Iterate Over a Vector" by Vasili Pascal (Optimum Web, 2023). URL: https://www.optimum-web.com/blog/iterators-in-rust-what-are-they-and-how-to-iterate-over-a-vector/
