Created
February 20, 2018 00:30
-
-
Save cbzehner/b14700279d4718d293e122016c446c65 to your computer and use it in GitHub Desktop.
Implement largest with just the PartialOrd trait bound
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn largest<T: PartialOrd>(list: &[T]) -> &T { | |
let mut largest = &list[0]; | |
for ref item in list.iter() { | |
if *item > largest { | |
largest = item; | |
} | |
} | |
largest | |
} | |
fn main() { | |
let number_list = vec![34, 50, 25, 100, 65]; | |
let result = largest(&number_list); | |
println!("The largest number is {}", result); | |
let char_list = vec!['y', 'm', 'a', 'q']; | |
let result = largest(&char_list); | |
println!("The largest char is {}", result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment