diff options
Diffstat (limited to 'rust/theBook/chapter-10-generic-types-traits-lifetimes/non-generic-largest.rs')
-rw-r--r-- | rust/theBook/chapter-10-generic-types-traits-lifetimes/non-generic-largest.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/rust/theBook/chapter-10-generic-types-traits-lifetimes/non-generic-largest.rs b/rust/theBook/chapter-10-generic-types-traits-lifetimes/non-generic-largest.rs new file mode 100644 index 0000000..149561e --- /dev/null +++ b/rust/theBook/chapter-10-generic-types-traits-lifetimes/non-generic-largest.rs @@ -0,0 +1,34 @@ +fn largest_i32(list: &[i32]) -> &i32 { + let mut largest = &list[0]; + + for item in list { + if largest < item { + largest = item; + } + } + + largest +} + +fn largest_char(list: &[char]) -> &char { + let mut largest = &list[0]; + + for item in list { + if largest < item { + largest = item; + } + } + + largest +} + +fn main() { + let l1 = [1,3,5,7,9]; + + let l2 = ['a','b','c','d','e']; + + let m1 = largest_i32(&l1); + let m2 = largest_char(&l2); + + println!("{m1}, {m2}"); +} |