From 37e6474818cf7b20a3a12f776c1faa4411c4cb37 Mon Sep 17 00:00:00 2001 From: 0xChasingFire <0xchasingfire@gmail.com> Date: Mon, 24 May 2021 12:27:17 +0800 Subject: [PATCH] err/generics/option/quiz/test/traits --- exercises/error_handling/result1.rs | 10 +++++++--- exercises/generics/generics1.rs | 4 +--- exercises/generics/generics2.rs | 10 ++++------ exercises/generics/generics3.rs | 10 ++++------ exercises/option/option1.rs | 10 ++++------ exercises/option/option2.rs | 6 ++---- exercises/option/option3.rs | 4 +--- exercises/quiz3.rs | 7 +++---- exercises/tests/tests1.rs | 3 +-- exercises/tests/tests2.rs | 3 +-- exercises/tests/tests3.rs | 6 ++---- exercises/traits/traits1.rs | 6 ++++-- exercises/traits/traits2.rs | 8 ++++++-- 13 files changed, 40 insertions(+), 47 deletions(-) diff --git a/exercises/error_handling/result1.rs b/exercises/error_handling/result1.rs index b978001..040be62 100644 --- a/exercises/error_handling/result1.rs +++ b/exercises/error_handling/result1.rs @@ -1,8 +1,6 @@ // result1.rs // Make this test pass! Execute `rustlings hint result1` for hints :) -// I AM NOT DONE - #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); @@ -14,7 +12,13 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> { - Ok(PositiveNonzeroInteger(value as u64)) + if value > 0{ + Ok(PositiveNonzeroInteger(value as u64)) + } else if value < 0 { + Err(CreationError::Negative) + } else { + Err(CreationError::Zero) + } } } diff --git a/exercises/generics/generics1.rs b/exercises/generics/generics1.rs index f93e64a..59432e0 100644 --- a/exercises/generics/generics1.rs +++ b/exercises/generics/generics1.rs @@ -3,9 +3,7 @@ // Execute `rustlings hint generics1` for hints! -// I AM NOT DONE - fn main() { - let mut shopping_list: Vec<?> = Vec::new(); + let mut shopping_list: Vec<&str> = Vec::new(); shopping_list.push("milk"); } diff --git a/exercises/generics/generics2.rs b/exercises/generics/generics2.rs index 1501529..8c6a510 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<T> { + value: T, } -impl Wrapper { - pub fn new(value: u32) -> Self { +impl<T> Wrapper<T> { + pub fn new(value: T) -> Self { Wrapper { value } } } diff --git a/exercises/generics/generics3.rs b/exercises/generics/generics3.rs index 64dd9bc..d197a23 100644 --- a/exercises/generics/generics3.rs +++ b/exercises/generics/generics3.rs @@ -10,15 +10,13 @@ // Execute 'rustlings hint generics3' for hints! -// I AM NOT DONE - -pub struct ReportCard { - pub grade: f32, +pub struct ReportCard<T: std::fmt::Display> { + pub grade: T, pub student_name: String, pub student_age: u8, } -impl ReportCard { +impl<T: std::fmt::Display> ReportCard<T> { pub fn print(&self) -> String { format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade) @@ -46,7 +44,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+", student_name: "Gary Plotter".to_string(), student_age: 11, }; diff --git a/exercises/option/option1.rs b/exercises/option/option1.rs index 602ff1a..6f068cf 100644 --- a/exercises/option/option1.rs +++ b/exercises/option/option1.rs @@ -1,23 +1,21 @@ // option1.rs // Make me compile! Execute `rustlings hint option1` for hints -// I AM NOT DONE - // you can modify anything EXCEPT for this function's sig fn print_number(maybe_number: Option<u16>) { 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<u16>; 5]; + let mut numbers: [Option<u16>; 5] = [Some(0); 5]; for iter in 0..5 { 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 c6b83ec..86656f6 100644 --- a/exercises/option/option2.rs +++ b/exercises/option/option2.rs @@ -1,12 +1,10 @@ // 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 { + if let Some(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<T> // You can stack `Option<T>`'s into while let and if let - integer = optional_integers_vec.pop() { + while let Some(Some(integer)) = optional_integers_vec.pop() { println!("current value: {}", integer); } } diff --git a/exercises/option/option3.rs b/exercises/option/option3.rs index 045d2ac..cb983ec 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<Point> = 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/quiz3.rs b/exercises/quiz3.rs index fae0eed..005fabe 100644 --- a/exercises/quiz3.rs +++ b/exercises/quiz3.rs @@ -7,8 +7,6 @@ // we expect to get when we call `times_two` with a negative number. // No hints, you can do this :) -// I AM NOT DONE - pub fn times_two(num: i32) -> i32 { num * 2 } @@ -19,12 +17,13 @@ mod tests { #[test] fn returns_twice_of_positive_numbers() { - assert_eq!(times_two(4), ???); + assert_eq!(times_two(4), 8); } #[test] fn returns_twice_of_negative_numbers() { // TODO replace unimplemented!() with an assert for `times_two(-4)` - unimplemented!() + // unimplemented!() + assert_eq!(times_two(-3), -6) } } diff --git a/exercises/tests/tests1.rs b/exercises/tests/tests1.rs index 50586a1..955f423 100644 --- a/exercises/tests/tests1.rs +++ b/exercises/tests/tests1.rs @@ -6,12 +6,11 @@ // This test has a problem with it -- make the test compile! Make the test // pass! Make the test fail! Execute `rustlings hint tests1` for hints :) -// I AM NOT DONE #[cfg(test)] mod tests { #[test] fn you_can_assert() { - assert!(); + assert!(true); } } diff --git a/exercises/tests/tests2.rs b/exercises/tests/tests2.rs index 0d981ad..b3cc15f 100644 --- a/exercises/tests/tests2.rs +++ b/exercises/tests/tests2.rs @@ -2,12 +2,11 @@ // This test has a problem with it -- make the test compile! Make the test // pass! Make the test fail! Execute `rustlings hint tests2` for hints :) -// I AM NOT DONE #[cfg(test)] mod tests { #[test] fn you_can_assert_eq() { - assert_eq!(); + assert_eq!(1, 1); } } diff --git a/exercises/tests/tests3.rs b/exercises/tests/tests3.rs index 3424f94..81eae9e 100644 --- a/exercises/tests/tests3.rs +++ b/exercises/tests/tests3.rs @@ -4,8 +4,6 @@ // we expect to get when we call `is_even(5)`. // Execute `rustlings hint tests3` for hints :) -// I AM NOT DONE - pub fn is_even(num: i32) -> bool { num % 2 == 0 } @@ -16,11 +14,11 @@ mod tests { #[test] fn is_true_when_even() { - assert!(); + assert!(is_even(4)); } #[test] fn is_false_when_odd() { - assert!(); + assert!(!is_even(3)); } } diff --git a/exercises/traits/traits1.rs b/exercises/traits/traits1.rs index 2ef9e11..d94eff7 100644 --- a/exercises/traits/traits1.rs +++ b/exercises/traits/traits1.rs @@ -8,14 +8,16 @@ // 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{ + // format!("{}{}", String::from(self), "Bar") + self + "Bar" + } } fn main() { diff --git a/exercises/traits/traits2.rs b/exercises/traits/traits2.rs index 916c3c4..bc41653 100644 --- a/exercises/traits/traits2.rs +++ b/exercises/traits/traits2.rs @@ -10,13 +10,17 @@ // No boiler plate code this time, // you can do this! -// I AM NOT DONE - trait AppendBar { fn append_bar(self) -> Self; } //TODO: Add your code here +impl AppendBar for Vec<String>{ + fn append_bar(mut self) -> Vec<String> { + self.push("Bar".to_string()); + self + } +} #[cfg(test)] mod tests {