diff options
author | Jordan Gong <jordan.gong@protonmail.com> | 2020-09-03 14:34:26 +0800 |
---|---|---|
committer | Jordan Gong <jordan.gong@protonmail.com> | 2020-09-03 14:34:26 +0800 |
commit | 638f6d997e76f45a665e2de741e6c4b2b5616aef (patch) | |
tree | 73fc1fcdab767d71970c1ff561aeb74c251a234f /largest/src | |
parent | 5626a54d626cfe520ba879c42fc9dd0331c76eb9 (diff) |
Find the largest T
Diffstat (limited to 'largest/src')
-rw-r--r-- | largest/src/main.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/largest/src/main.rs b/largest/src/main.rs new file mode 100644 index 0000000..fbbf4d4 --- /dev/null +++ b/largest/src/main.rs @@ -0,0 +1,23 @@ +fn largest<T: PartialOrd + Copy>(list: &[T]) -> T { + let mut largest = list[0]; + + for &item in list { + 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); +} |