use std::io;
fn read()->i32 {
let mut num=String::new();
io::stdin()
.read_line(&mut num)
.expect("Error");
num.trim()
.parse::()
.unwrap()
}
fn main() {
println!("Enter number of elements of an array");
let n = read() as usize;
let mut a = vec!();
println!("Enter elements");
for i in 0..n {
let x = read();
a.push(x);
}
for i in 0..n {
if a[0] < a[i] {
a[0] = a[i];
}
}
println!("Largest element = {}",a[0]);
Taking an input of number of elements of an array and elements of it then, comparing the each element of an array with the other elements and printing the largest element of an array.
use std::io; fn read()->i32 { let mut num=String::new(); io::stdin() .read_line(&mut num) .expect("Error"); num.trim() .parse::()
.unwrap()
}
fn main() { println!("Enter number of elements of an array"); let n = read() as usize; let mut a = vec!(); println!("Enter elements"); for i in 0..n { let x = read(); a.push(x); } for i in 0..n { if a[0] < a[i] { a[0] = a[i]; } } println!("Largest element = {}",a[0]);
}