Trending September 2023 # Working Of Iterator In Rust With Examples # Suggested October 2023 # Top 11 Popular | Nhahang12h.com

Trending September 2023 # Working Of Iterator In Rust With Examples # Suggested October 2023 # Top 11 Popular

You are reading the article Working Of Iterator In Rust With Examples updated in September 2023 on the website Nhahang12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Working Of Iterator In Rust With Examples

Introduction to Rust Iterator

Whenever we want to iterate over a collection of values like vectors, maps, arrays, etc., we use iterators in Rust. The iterator implements the iterator trait defined in the standard library of Rust. The values in the iterator object are called items, and the items in the iterator can be traversed using the next() method. This 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.

Start Your Free Software Development Course

The syntax to declare an 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 implements the iterator trait defined in the standard library of Rust.

The values in the iterator object are called items.

To go through the items in the iterator, just utilize the next() method.

The next() method returns a None value when the iterator reaches the end of the collection.

Once you implement the iterator trait, you can traverse the collection easily by using a for loop and the “in” keyword.

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 the next() method to traverse through the next elements in the array and display the output on the screen:

Code:

fn main() { let arrayname = ["Welcome","to","EDUCBA"]; let mut iteratorname = arrayname.iter(); println!("The items in the collection are:n"); println!("{:?}",iteratorname); println!("n"); 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:

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() { let arrayname = ["Welcome","to","EDUCBA"]; let iteratorname = arrayname.iter(); println!("The items in the collection are:n"); println!("{:?}",iteratorname); println!("n"); 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 define an iterator trait and store it in a mutable variable called iterator name. We print all the array elements 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 and a keyword. The snapshot provided above shows the output.

Example #3

Code:

fn main() { let arrayname = ["Learning","is","fun"]; let iteratorname = arrayname.iter(); println!("The items in the collection are:n"); println!("{:?}",iteratorname); println!("n"); 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 define an iterator trait and store it in a mutable variable called iteratorname. Then we print all the array elements using the iterator object as the output on the screen. Next, by iterating through the collection using a for loop and the in keyword, each individual item in the collection will be displayed. The output is shown in the snapshot above.

Conclusion

In this article, we have learned the concept of an iterator in Rust-through definition, syntax, and working of an iterator in Rust with corresponding programming examples and their outputs to demonstrate them.

Recommended Articles

We hope that this EDUCBA information on “Rust Iterator” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

You're reading Working Of Iterator In Rust With Examples

Update the detailed information about Working Of Iterator In Rust With Examples on the Nhahang12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!