Introduction to Rust Iterator
Whenever we want to iterate over a collection of values like vectors, maps, arrays, etc. we make use of iterators in Rust, and the iterator trait defined in the standard library of Rust is implemented by the iterator, and the values in the iterator object are called items and the items in the iterator can be traversed using a method called next() method and this method returns a None value when the iterator reaches the end of the collection and once the iterator trait is implemented, the collection can be traversed using for loop with in keyword as well.
The syntax to declare iterator in Rust is as follows:
let mut iterator_name = collection_name.iter();
Where mut is the keyword to represent a mutable variable,
- iterator_name is the name of the iterator.
- collection_name is the name of the collection of arrays, maps, or vectors.
Working of Iterator in Rust
Working of iterator in Rust is as follows:
- A sequence of values can be produced using objects called iterators so that they can be looped over or iterated.
- The iterator can iterate over a collection of values such as maps, arrays, vectors, etc.
- The iterator trait defined in the standard library of Rust is implemented by the iterator.
- The values in the iterator object are called items.
- The items in the iterator can be traversed using a method called the next() method.
- The next() method returns a None value when the iterator reaches the end of the collection.
- Once the iterator trait is implemented, the collection can be traversed using for loop with in keyword as well.
Examples
Let us discuss examples of Rust Iterator.
Example #1
Rust program to demonstrate iterator in which we declare an array and use the iterator to read the values from the array and make use of next() method to traverse through the next elements in the array and display the output on the screen:
Code:
fn main()
{
//declaring an array of strings and storing it in a variable called arrayname
let arrayname = ["Welcome","to","EDUCBA"];
//defining an iterator trait and storing it in a mutable variable called iteratorname
let mut iteratorname = arrayname.iter();
//printing all the elements of the array using iterator object
println!("The items in the collection are:\n");
println!("{:?}",iteratorname);
println!("\n");
//displaying individual items in the collection by traversing through the collection until the end of the collection is reached
println!("The individual items in the collection are:\n");
println!("{:?}",iteratorname.next());
println!("{:?}",iteratorname.next());
println!("{:?}",iteratorname.next());
println!("{:?}",iteratorname.next());
}
The output of the above program is as shown in the snapshot below:
In the above program, we are declaring an array of strings and storing it in a variable called array name. Then we are defining an iterator trait and store it in a mutable variable called iterator name. Then we are printing all the elements of the array using the iterator object as the output on the screen. Then displaying individual items in the collection by traversing through the collection until the end of the collection is reached. The output is shown in the snapshot above.
Example #2
Rust program to demonstrate iterator in which we declare an array and use the iterator to read the values from the array and make use of for loop along with in keyword to read each item in the array and display the output on the screen:
Code:
fn main()
{
//declaring an array of strings and storing it in a variable called arrayname
let arrayname = ["Welcome","to","EDUCBA"];
//defining an iterator trait and storing it in a mutable variable called iteratorname
let iteratorname = arrayname.iter();
//printing all the elements of the array using iterator object
println!("The items in the collection are:\n");
println!("{:?}",iteratorname);
println!("\n");
//displaying individual items in the collection by traversing through the collection using for loop
println!("The individual items in the collection are:\n");
for eachitem in iteratorname
{
print!("{}\t",eachitem);
}
}
The output of the above program is as shown in the snapshot below:
In the above program, we are declaring an array of strings and storing it in a variable called array name. Then we are defining an iterator trait and store it in a mutable variable called iterator name. Then we are printing all the elements of the array using the iterator object as the output on the screen. Then displaying individual items in the collection by traversing through the collection using for loop along within keyword. The output is shown in the snapshot above.
Example #3
Rust program to demonstrate iterator in which we declare an array and use the iterator to read the values from the array and make use of for loop along within keyword to read each item in the array and display the output on the screen:
Code:
fn main()
{
//declaring an array of strings and storing it in a variable called arrayname
let arrayname = ["Learning","is","fun"];
//defining an iterator trait and storing it in a mutable variable called iteratorname
let iteratorname = arrayname.iter();
//printing all the elements of the array using iterator object
println!("The items in the collection are:\n");
println!("{:?}",iteratorname);
println!("\n");
//displaying individual items in the collection by traversing through the collection using for loop
println!("The individual items in the collection are:\n");
for eachitem in iteratorname
{
print!("{}\t",eachitem);
}
}
The output of the above program is as shown in the snapshot below:
In the above program, we are declaring an array of strings and storing it in a variable called arrayname. Then we are defining an iterator trait and store it in a mutable variable called iteratorname. Then we are printing all the elements of the array using the iterator object as the output on the screen. Then displaying individual items in the collection by traversing through the collection using for loop along with in keyword. The output is shown in the snapshot above.
Conclusion
In this article, we have learned the concept of iterator in Rust-through definition, syntax, and working of iterator in Rust with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
This is a guide to Rust Iterator. Here we discuss the Introduction, syntax, Working of Iterator in Rust, examples with code implementation. You may also have a look at the following articles to learn more –
41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses