From 76be5e4e991160f5fd9093f03ee2ba260e8f7229 Mon Sep 17 00:00:00 2001 From: sjmann Date: Fri, 28 Feb 2020 00:09:08 +0000 Subject: [PATCH 1/3] feat: added new exercises for generics --- exercises/generics/README.md | 7 +++++ exercises/generics/generics1.rs | 10 ++++++++ exercises/generics/generics2.rs | 28 ++++++++++++++++++++ exercises/generics/generics3.rs | 45 +++++++++++++++++++++++++++++++++ info.toml | 32 +++++++++++++++++++++++ 5 files changed, 122 insertions(+) create mode 100644 exercises/generics/README.md create mode 100644 exercises/generics/generics1.rs create mode 100644 exercises/generics/generics2.rs create mode 100644 exercises/generics/generics3.rs diff --git a/exercises/generics/README.md b/exercises/generics/README.md new file mode 100644 index 00000000..7105f06f --- /dev/null +++ b/exercises/generics/README.md @@ -0,0 +1,7 @@ +### Generics + +In this section you'll learn about saving yourself many lines of code with generics! + +### Book Sections + +- [Generic Data Types](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html) \ No newline at end of file diff --git a/exercises/generics/generics1.rs b/exercises/generics/generics1.rs new file mode 100644 index 00000000..d075a4d2 --- /dev/null +++ b/exercises/generics/generics1.rs @@ -0,0 +1,10 @@ +// 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(); + shopping_list.push("milk"); +} + diff --git a/exercises/generics/generics2.rs b/exercises/generics/generics2.rs new file mode 100644 index 00000000..7b9bfc4f --- /dev/null +++ b/exercises/generics/generics2.rs @@ -0,0 +1,28 @@ +// This powerful wrapper provides the ability to store a positive integer value. +// Rewrite it using generics so that it supports wrapping ANY type. +struct Wrapper { + value: u32 +} + +impl Wrapper { + pub fn new(value: u32) -> Self { + Wrapper { value } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn store_u32_in_wrapper() { + 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"); + } +} \ No newline at end of file diff --git a/exercises/generics/generics3.rs b/exercises/generics/generics3.rs new file mode 100644 index 00000000..e19142d2 --- /dev/null +++ b/exercises/generics/generics3.rs @@ -0,0 +1,45 @@ +// 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 +// 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. + +pub struct ReportCard { + pub grade: f32, + 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) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn generate_numeric_report_card() { + let report_card = ReportCard { + 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"); + } + + #[test] + 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(), + 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/info.toml b/info.toml index 178a6f42..8c8b1a54 100644 --- a/info.toml +++ b/info.toml @@ -608,6 +608,38 @@ Try mutating the incoming string vector. Vectors provide suitable methods for adding an element at the end. See the documentation at: https://doc.rust-lang.org/std/vec/struct.Vec.html""" +# Generics + +[[exercises]] +name = "generics1" +path = "exercises/generics/generics1.rs" +mode = "compile" +hint = """ +Vectors in rust make use of generics to create dynamically sized arrays of any type. +You need to tell the compiler what type we are pushing onto this vector.""" + +[[exercises]] +name = "generics2" +path = "exercises/generics/generics2.rs" +mode = "test" +hint = """ +Think carefully about what we need to do here. Currently we are wrapping only values of +type 'u32'. Maybe we need to update the explicit references to this data type somehow? +""" + +[[exercises]] +name = "generics3" +path = "exercises/generics/generics3_solution.rs" +mode = "test" +hint = """ +To find the best solution to this challenge you're going to need to think back to your +knowledge of traits, specifically Trait Bound Syntax - you may also need this: "use std::fmt::Display;" + +This is definitely harder than the last two exercises! You need to think about not only making the +ReportCard struct generic, but also the correct property - you will need to change the implementation +of the struct slightly too...you can do it! +""" + # THREADS [[exercises]] From 5b6e23c323a320ca8306c7645bead2132590624e Mon Sep 17 00:00:00 2001 From: sjmann Date: Fri, 28 Feb 2020 00:29:30 +0000 Subject: [PATCH 2/3] removed artifact from manual testing --- info.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/info.toml b/info.toml index d0ed053e..aff47ea1 100644 --- a/info.toml +++ b/info.toml @@ -645,7 +645,7 @@ type 'u32'. Maybe we need to update the explicit references to this data type so [[exercises]] name = "generics3" -path = "exercises/generics/generics3_solution.rs" +path = "exercises/generics/generics3.rs" mode = "test" hint = """ To find the best solution to this challenge you're going to need to think back to your From 0f8001ea449c278faea0e731b49a5eb3dcd28634 Mon Sep 17 00:00:00 2001 From: sjmann Date: Fri, 28 Feb 2020 00:31:55 +0000 Subject: [PATCH 3/3] add I AM NOT DONE comments --- exercises/generics/generics2.rs | 2 ++ exercises/generics/generics3.rs | 1 + 2 files changed, 3 insertions(+) diff --git a/exercises/generics/generics2.rs b/exercises/generics/generics2.rs index 7b9bfc4f..1fe5041f 100644 --- a/exercises/generics/generics2.rs +++ b/exercises/generics/generics2.rs @@ -1,5 +1,7 @@ // 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 { value: u32 } diff --git a/exercises/generics/generics3.rs b/exercises/generics/generics3.rs index e19142d2..cd3232a4 100644 --- a/exercises/generics/generics3.rs +++ b/exercises/generics/generics3.rs @@ -6,6 +6,7 @@ // Make the necessary code changes to support alphabetical report cards, thereby making the second // test pass. +// I AM NOT DONE pub struct ReportCard { pub grade: f32, pub student_name: String,