Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now