From 5ea5b879c2bc3b30ffe56d7806752bf727f98236 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Tue, 24 Aug 2021 07:21:03 +0200 Subject: [PATCH] Generics and Traits --- src/main.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/main.rs b/src/main.rs index c07c405..a4e4f1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -116,9 +116,77 @@ fn test_files() { } } +// needs Copy trait, good for simple types +fn largest(list: &[T]) -> T { + let mut result = list[0]; + for &i in list { + if i > result { + result = i; + } + } + result +} + +// expensive for large strings, don't use that +fn largest_clone(list: &[T]) -> T { + let mut result = list[0].clone(); + for i in list { + if *i > result { + result = i.clone(); + } + } + result +} + +// good for everything, but more expensive for simple types +fn largest_ref(list: &[T]) -> &T { + let mut result = &list[0]; + for i in list { + if i > result { + result = i; + } + } + result +} + +fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { + if x.len() > y.len() { + x + } else { + y + } +} + +fn test_generics() { + let num_list = vec![3, 42, -7, 100, 0]; + println!("largest number: {}", largest(&num_list)); + println!("num_list: {:?}", num_list); + + let char_list = vec!['a', 'y', 'q', 'm']; + println!("largest char: {}", largest(&char_list)); + + let str_list = vec!["hello", "world", "blue", "planet"]; + println!("largest str: {}", largest(&str_list)); + println!("str_list: {:?}", str_list); + + let string_list = vec!["aaaa".to_string(), "xxxxx".to_string(), "ffff".to_string()]; + println!("largest string (with cloning): {}", largest_clone(&string_list)); + println!("largest string (with ref): {}", largest_ref(&string_list)); + println!("string_list: {:?}", string_list); + + let s1 = String::from("abcd"); + let l; + { + let s2 = "efghi"; + l = longest(&s1, s2); + } + println!("longest string: {}", l); +} + fn main() { test_strings(); test_vectors(); test_hashmaps(); test_files(); + test_generics(); } -- 2.39.5