ADVERTISEMENT
ADVERTISEMENT

Rust program to use an iterator to read values from an array

Rust | Iterator and Closure Example: Write a program to demonstrate the use of an iterator to read values from an array.
Submitted by Nidhi, on November 22, 2021

Problem Solution:

In this program, we will create an array of integers and read values from an array using iterator and print elements.

Program/Source Code:

The source code to use an iterator to read values from an array is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to use of an iterator 
// to read values from an array

fn main() 
{
   let int_arr = [50,40,30,20,10];
   let mut itr = int_arr.iter(); 
   
   println!("Array elements are: ");
   println!("{:?}",itr.next());
   println!("{:?}",itr.next());
   println!("{:?}",itr.next());
   println!("{:?}",itr.next());
   println!("{:?}",itr.next());
}

Output:

Array elements are: 
Some(50)
Some(40)
Some(30)
Some(20)
Some(10)

Explanation:

Here, we created an array of integers. Then we read the elements of an array using iterator and printed the result.

Rust Iterator and Closure Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.