diff --git a/exercises/generics/generics1.rs b/exercises/generics/generics1.rs index d075a4d..fc244ad 100644 --- a/exercises/generics/generics1.rs +++ b/exercises/generics/generics1.rs @@ -1,10 +1,8 @@ -// This shopping list program isn't compiling! +// This shopping list program isn't compiling! // Use your knowledge of generics to fix it. -// 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 1fe5041..1ccaf22 100644 --- a/exercises/generics/generics2.rs +++ b/exercises/generics/generics2.rs @@ -1,13 +1,12 @@ // This powerful wrapper provides the ability to store a positive integer value. // Rewrite it using generics so that it supports wrapping ANY type. -// I AM NOT DONE -struct Wrapper<u32> { - value: u32 +struct Wrapper<T> { + value: T } -impl<u32> Wrapper<u32> { - pub fn new(value: u32) -> Self { +impl<T> Wrapper<T> { + pub fn new(value: T) -> Self { Wrapper { value } } } @@ -18,13 +17,11 @@ mod tests { #[test] fn store_u32_in_wrapper() { - assert_eq!(Wrapper::new(42).value, 42); + assert_eq!(Wrapper::new(42).value, 42); } #[test] fn store_str_in_wrapper() { - // TODO: Delete this assert and uncomment the one below once you have finished the exercise. - assert!(false); - // assert_eq!(Wrapper::new("Foo").value, "Foo"); + assert_eq!(Wrapper::new("Foo").value, "Foo"); } -} \ No newline at end of file +} diff --git a/exercises/generics/generics3.rs b/exercises/generics/generics3.rs index cd3232a..cab0515 100644 --- a/exercises/generics/generics3.rs +++ b/exercises/generics/generics3.rs @@ -1,19 +1,19 @@ // An imaginary magical school has a new report card generation system written in rust! -// Currently the system only supports creating report cards where the student's grade +// Currently the system only supports creating report cards where the student's grade // is represented numerically (e.g. 1.0 -> 5.5). However, the school also issues alphabetical grades // (A+ -> F-) and needs to be able to print both types of report card! // Make the necessary code changes to support alphabetical report cards, thereby making the second // test pass. +use std::fmt::Display; -// I AM NOT DONE -pub struct ReportCard { - pub grade: f32, +pub struct ReportCard<T> { + pub grade: T, pub student_name: String, pub student_age: u8, } -impl ReportCard { +impl<T:Display> ReportCard<T> { pub fn print(&self) -> String { format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade) } @@ -26,8 +26,8 @@ mod tests { #[test] fn generate_numeric_report_card() { let report_card = ReportCard { - grade: 2.1, - student_name: "Tom Wriggle".to_string(), + grade: 2.1, + student_name: "Tom Wriggle".to_string(), student_age: 12, }; assert_eq!(report_card.print(), "Tom Wriggle (12) - achieved a grade of 2.1"); @@ -37,10 +37,10 @@ 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, - student_name: "Gary Plotter".to_string(), + grade: "A+", + student_name: "Gary Plotter".to_string(), student_age: 11, }; assert_eq!(report_card.print(), "Gary Plotter (11) - achieved a grade of A+"); } -} \ No newline at end of file +} diff --git a/exercises/standard_library_types/iterators3.rs b/exercises/standard_library_types/iterators3.rs index 4bed64f..2a79d17 100644 --- a/exercises/standard_library_types/iterators3.rs +++ b/exercises/standard_library_types/iterators3.rs @@ -7,8 +7,6 @@ // Execute `rustlings hint iterators3` to get some hints! // Have fun :-) -// I AM NOT DONE - #[derive(Debug, PartialEq, Eq)] pub enum DivisionError { NotDivisible(NotDivisibleError), @@ -70,12 +68,11 @@ mod tests { } // Iterator exercises using your `divide` function - /* #[test] fn result_with_list() { let numbers = vec![27, 297, 38502, 81]; let division_results = numbers.into_iter().map(|n| divide(n, 27)); - let x //... Fill in here! + let x = division_results.collect::<Result<Vec<_>, _>>(); assert_eq!(format!("{:?}", x), "Ok([1, 11, 1426, 3])"); } @@ -83,8 +80,7 @@ mod tests { fn list_of_results() { let numbers = vec![27, 297, 38502, 81]; let division_results = numbers.into_iter().map(|n| divide(n, 27)); - let x //... Fill in here! + let x = division_results.collect::<Vec<_>>(); assert_eq!(format!("{:?}", x), "[Ok(1), Ok(11), Ok(1426), Ok(3)]"); } - */ } diff --git a/exercises/standard_library_types/iterators4.rs b/exercises/standard_library_types/iterators4.rs index 8886283..df433dd 100644 --- a/exercises/standard_library_types/iterators4.rs +++ b/exercises/standard_library_types/iterators4.rs @@ -1,7 +1,5 @@ // iterators4.rs -// I AM NOT DONE - pub fn factorial(num: u64) -> u64 { // Complete this function to return the factorial of num // Do not use: @@ -12,6 +10,7 @@ pub fn factorial(num: u64) -> u64 { // For an extra challenge, don't use: // - recursion // Execute `rustlings hint iterators4` for hints. + (1..num+1).fold(1, |m, x| m * x) } #[cfg(test)] diff --git a/exercises/traits/traits1.rs b/exercises/traits/traits1.rs index 8253ef8..b5c2284 100644 --- a/exercises/traits/traits1.rs +++ b/exercises/traits/traits1.rs @@ -1,21 +1,22 @@ // traits1.rs // Time to implement some traits! -// +// // Your task is to implement the trait // `AppendBar' for the type `String'. -// +// // The trait AppendBar has only one function, // 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(mut self) -> Self { + self.push_str("Bar"); + self + } } fn main() { @@ -41,4 +42,4 @@ mod tests { ); } -} \ No newline at end of file +} diff --git a/exercises/traits/traits2.rs b/exercises/traits/traits2.rs index 7f5014d..d42f207 100644 --- a/exercises/traits/traits2.rs +++ b/exercises/traits/traits2.rs @@ -1,25 +1,25 @@ // traits2.rs -// +// // Your task is to implement the trait // `AppendBar' for a vector of strings. -// +// // To implement this trait, consider for // a moment what it means to 'append "Bar"' // to a vector of strings. -// +// // 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) -> Self { + self.push(String::from("Bar")); + self + } +} #[cfg(test)] mod tests {