diff --git a/ch10/extract_fn b/ch10/extract_fn new file mode 100755 index 0000000..847d90d Binary files /dev/null and b/ch10/extract_fn differ diff --git a/ch10/extract_fn.rs b/ch10/extract_fn.rs new file mode 100644 index 0000000..5995935 --- /dev/null +++ b/ch10/extract_fn.rs @@ -0,0 +1,21 @@ +fn largest(list: &[i32]) -> &i32 { + let mut largest = &list[0]; + + for num in list { + if num > largest { + largest = num; + } + } + + largest +} + +fn main() { + let number_list = vec![30, 52, 12, 69, 23, 50]; + let large = largest(&number_list); + println!("Largest: {large}"); + + let number_list = vec![130, 52, 12, 69, 23, 50]; + let large = largest(&number_list); + println!("Largest: {large}"); +} diff --git a/ch10/lifetime_hypotheses b/ch10/lifetime_hypotheses new file mode 100755 index 0000000..80ca7e0 Binary files /dev/null and b/ch10/lifetime_hypotheses differ diff --git a/ch10/lifetime_hypotheses.rs b/ch10/lifetime_hypotheses.rs new file mode 100644 index 0000000..cb7a050 --- /dev/null +++ b/ch10/lifetime_hypotheses.rs @@ -0,0 +1,22 @@ +use std::io; + +fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { + if x.len() > y.len() { x } else { y } +} + +fn brap<'b>(x: &'b str, y: &str) -> &'b str { + if x.len() > y.len() { x } else { "henkieeeeee" } +} + +fn main() { + let stronk = String::from("stronkman"); + + let ln; + { + let stonks = "stonks"; + let mut inp = String::new(); + io::stdin().read_line(&mut inp).expect("Please input a name"); + ln = longest(&inp, brap(stronk.as_str(), stonks)); + } + println!("The longest of them all: {}", ln); +} diff --git a/ch10/trait_blanket_implementation b/ch10/trait_blanket_implementation new file mode 100755 index 0000000..e1db53a Binary files /dev/null and b/ch10/trait_blanket_implementation differ diff --git a/ch10/trait_blanket_implementation.rs b/ch10/trait_blanket_implementation.rs new file mode 100644 index 0000000..7c93b3f --- /dev/null +++ b/ch10/trait_blanket_implementation.rs @@ -0,0 +1,29 @@ +trait Named { + fn say_name(&self) -> String; +} + +trait Bold { + fn write_bold(&self); +} + +impl Bold for T { + fn write_bold(&self) { + println!("!bold {}", self.say_name()) + } +} + +struct Person { + name: String, +} + +impl Named for Person { + fn say_name(&self) -> String { + self.name.clone() + } +} + +fn main() { + let p = Person{name: String::from("Jasper")}; + + p.write_bold(); +}