Rust program to find the largest element from the array

Rust | Array Example: Write a program to find the largest element from the array.
Submitted by Nidhi, on October 20, 2021

Problem Solution:

In this program, we will create an integer array with 5 elements then we will find the largest element from the array.

Program/Source Code:

The source code to find the largest element from the array is given below. The given program is compiled and executed successfully.

// Rust program to find the largest element 
// from array

fn main() {
   let arr:[i32;5] = [1,2,23,4,5];
   let mut large:i32 = 0;
   let mut i:usize = 0;
   
   large=arr[0];
   while i<arr.len()
   {
		if large < arr[i] {
			large = arr[i]
		}
		i = i + 1;
   }
   println!("Largest element is: {}", large);
}

Output:

Largest element is: 23

Explanation:

Here, we created an array of the integer with 5 elements. Then we found the largest element from the array and printed the result.

Rust Arrays Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.