From 76be5e4e991160f5fd9093f03ee2ba260e8f7229 Mon Sep 17 00:00:00 2001 From: sjmann Date: Fri, 28 Feb 2020 00:09:08 +0000 Subject: [PATCH] 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]]