Q:

Rust program to access array elements using the \'for\' loop

belongs to collection: Rust Arrays Programs

0

In this program, we will create an integer array with 5 values. Then we will access each element of the array using the "for" loop and print them.

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 access array elements using the "for" loop is given below. The given program is compiled and executed successfully.

// Rust program to access array elements
// using "for" loop

fn main(){
   let intArr:[i32;5] = [1,2,3,4,5];
   
   println!("Array elements:");
   for i in 0..5 {
      print!("{} ",intArr[i]);
   }
}

Output:

Array elements:
1 2 3 4 5

Explanation:

Here, we created an array intArr with 5 integer values. Then we accessed array elements using the "for" loop and printed them.

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

total answers (1)

Rust Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to access array elements using iter()... >>
<< Rust program to initialize array elements with a d...