Skip to content

Instantly share code, notes, and snippets.

@anoopelias
Created January 14, 2017 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anoopelias/1fe49e1432ca9b7712fd5106a72ec46b to your computer and use it in GitHub Desktop.
Save anoopelias/1fe49e1432ca9b7712fd5106a72ec46b to your computer and use it in GitHub Desktop.
use std::cmp::max;
fn largest2(arr: [i32; 5]) -> i32 {
arr.iter().fold(0, |num, &x| {
max(num, x)
})
}
fn largest3(arr: [i32; 5]) -> i32 {
arr.iter().fold(0, max) // Do Not compile!
}
fn main() {
let arr = [5, 6, 2, 9, 3];
let l2 = largest2(arr);
let l3 = largest3(arr);
println!("Largest2 : {}", l2);
println!("Largest3 : {}", l3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment