ADVERTISEMENT
ADVERTISEMENT

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

Rust | Array Example: Write a program to access array elements using the for loop.
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 "for" loop and print them.

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.

Rust Arrays 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.