Home »
Rust »
Rust Programs
Rust program to access array elements using iter() function
Rust | Array Example: Write a program to access array elements using iter() function.
Submitted by Nidhi, on October 19, 2021
Problem Solution:
In this program, we will create an integer array with 5 values. Then we will access each element of the array using the iter() function and print them.
Program/Source Code:
The source code to access array elements using the iter() function is given below. The given program is compiled and executed successfully.
// Rust program to access array elements
// using iter() function
fn main(){
let intArr:[i32;5] = [1,2,3,4,5];
println!("Array elements:");
for item in intArr.iter(){
print!("{} ",item);
}
}
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 iter() function and printed them.
Rust Arrays Programs »