From 12186f4a30016d23c6d17ac9a3bc952bcd32664f Mon Sep 17 00:00:00 2001 From: Chris Girvin Date: Sun, 22 May 2022 05:33:23 -0400 Subject: [PATCH] traits done --- exercises/generics/generics2.rs | 10 ++++------ exercises/generics/generics3.rs | 18 ++++++++++-------- exercises/option/option1.rs | 14 +++++--------- exercises/option/option2.rs | 10 ++++------ exercises/option/option3.rs | 4 +--- exercises/traits/traits1.rs | 7 +++++-- exercises/traits/traits2.rs | 6 ++++++ 7 files changed, 35 insertions(+), 34 deletions(-) diff --git a/exercises/generics/generics2.rs b/exercises/generics/generics2.rs index 1501529c..8c6a5100 100644 --- a/exercises/generics/generics2.rs +++ b/exercises/generics/generics2.rs @@ -3,14 +3,12 @@ // Execute `rustlings hint generics2` for hints! -// I AM NOT DONE - -struct Wrapper { - value: u32, +struct Wrapper { + value: T, } -impl Wrapper { - pub fn new(value: u32) -> Self { +impl Wrapper { + pub fn new(value: T) -> Self { Wrapper { value } } } diff --git a/exercises/generics/generics3.rs b/exercises/generics/generics3.rs index 64dd9bc1..c55669fa 100644 --- a/exercises/generics/generics3.rs +++ b/exercises/generics/generics3.rs @@ -10,18 +10,20 @@ // Execute 'rustlings hint generics3' for hints! -// I AM NOT DONE +use std::fmt::Display; -pub struct ReportCard { - pub grade: f32, +pub struct ReportCard { + pub grade: T, pub student_name: String, pub student_age: u8, } -impl ReportCard { - pub fn print(&self) -> String { - format!("{} ({}) - achieved a grade of {}", - &self.student_name, &self.student_age, &self.grade) +impl ReportCard { + fn print(&self) -> String { + format!( + "{} ({}) - achieved a grade of {}", + &self.student_name, &self.student_age, &self.grade + ) } } @@ -46,7 +48,7 @@ mod tests { fn generate_alphabetic_report_card() { // TODO: Make sure to change the grade here after you finish the exercise. let report_card = ReportCard { - grade: 2.1, + grade: "A+".to_string(), student_name: "Gary Plotter".to_string(), student_age: 11, }; diff --git a/exercises/option/option1.rs b/exercises/option/option1.rs index 17cf4f60..24776a83 100644 --- a/exercises/option/option1.rs +++ b/exercises/option/option1.rs @@ -1,23 +1,19 @@ // option1.rs // Make me compile! Execute `rustlings hint option1` for hints -// I AM NOT DONE - // you can modify anything EXCEPT for this function's signature fn print_number(maybe_number: Option) { println!("printing: {}", maybe_number.unwrap()); } fn main() { - print_number(13); - print_number(99); + print_number(Some(13)); + print_number(Some(99)); - let mut numbers: [Option; 5]; + let mut numbers: [Option; 5] = [Some(0); 5]; for iter in 0..5 { - let number_to_add: u16 = { - ((iter * 1235) + 2) / (4 * 16) - }; + let number_to_add: u16 = { ((iter * 1235) + 2) / (4 * 16) }; - numbers[iter as usize] = number_to_add; + numbers[iter as usize] = Some(number_to_add); } } diff --git a/exercises/option/option2.rs b/exercises/option/option2.rs index c6b83ece..c9815450 100644 --- a/exercises/option/option2.rs +++ b/exercises/option/option2.rs @@ -1,13 +1,11 @@ // option2.rs // Make me compile! Execute `rustlings hint option2` for hints -// I AM NOT DONE - fn main() { let optional_word = Some(String::from("rustlings")); // TODO: Make this an if let statement whose value is "Some" type - word = optional_word { - println!("The word is: {}", word); + if let word = optional_word { + println!("The word is: {:?}", word); } else { println!("The optional word doesn't contain anything"); } @@ -19,7 +17,7 @@ fn main() { // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option // You can stack `Option`'s into while let and if let - integer = optional_integers_vec.pop() { - println!("current value: {}", integer); + while let Some(integer) = optional_integers_vec.pop() { + println!("current value: {:?}", integer); } } diff --git a/exercises/option/option3.rs b/exercises/option/option3.rs index 045d2acb..cb983ec4 100644 --- a/exercises/option/option3.rs +++ b/exercises/option/option3.rs @@ -1,8 +1,6 @@ // option3.rs // Make me compile! Execute `rustlings hint option3` for hints -// I AM NOT DONE - struct Point { x: i32, y: i32, @@ -12,7 +10,7 @@ fn main() { let y: Option = Some(Point { x: 100, y: 200 }); match y { - Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), + Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y), _ => println!("no match"), } y; // Fix without deleting this line. diff --git a/exercises/traits/traits1.rs b/exercises/traits/traits1.rs index 15e08f24..d59d23e7 100644 --- a/exercises/traits/traits1.rs +++ b/exercises/traits/traits1.rs @@ -8,14 +8,17 @@ // which appends "Bar" to any object // implementing this trait. -// I AM NOT DONE - trait AppendBar { fn append_bar(self) -> Self; } impl AppendBar for String { //Add your code here + fn append_bar(self) -> String { + let mut out = String::from(&self); + out.push_str("Bar"); + out.to_string() + } } fn main() { diff --git a/exercises/traits/traits2.rs b/exercises/traits/traits2.rs index 916c3c4b..893d5321 100644 --- a/exercises/traits/traits2.rs +++ b/exercises/traits/traits2.rs @@ -17,6 +17,12 @@ trait AppendBar { } //TODO: Add your code here +impl AppendBar for Vec { + fn append_bar(mut self) -> Vec { + self.append(&mut vec!["Bar".to_string()]); + self + } +} #[cfg(test)] mod tests {