From fbc00d495ef60abc8716c2be57fb87071de9ed60 Mon Sep 17 00:00:00 2001 From: lukaszKielar Date: Tue, 30 Jun 2020 20:35:12 +0200 Subject: [PATCH] finish generics exercises --- exercises/generics/generics1.rs | 7 ++---- exercises/generics/generics2.rs | 13 +++++----- exercises/generics/generics3.rs | 42 ++++++++++++++++++++------------- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/exercises/generics/generics1.rs b/exercises/generics/generics1.rs index d075a4d2..07a3ed8c 100644 --- a/exercises/generics/generics1.rs +++ b/exercises/generics/generics1.rs @@ -1,10 +1,7 @@ -// 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 23025aaa..57ae9486 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 { - value: u32 +struct Wrapper { + value: T, } -impl Wrapper { - pub fn new(value: u32) -> Self { +impl Wrapper { + pub fn new(value: T) -> Self { Wrapper { value } } } @@ -18,11 +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() { 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 c76425c3..9ffdbd88 100644 --- a/exercises/generics/generics3.rs +++ b/exercises/generics/generics3.rs @@ -1,23 +1,25 @@ // 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 +// 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 +// Make the necessary code changes to support alphabetical report cards, thereby making // the second test pass. +use std::fmt::{Debug, Display}; -// I AM NOT DONE -pub struct ReportCard { - pub grade: f32, +pub struct ReportCard { + pub grade: T, pub student_name: String, pub student_age: u8, } -impl ReportCard { +impl ReportCard { pub fn print(&self) -> String { - format!("{} ({}) - achieved a grade of {}", - &self.student_name, &self.student_age, &self.grade) + format!( + "{} ({}) - achieved a grade of {}", + &self.student_name, &self.student_age, &self.grade + ) } } @@ -28,21 +30,27 @@ 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"); + 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(), + grade: "A+".to_string(), + student_name: "Gary Plotter".to_string(), student_age: 11, }; - assert_eq!(report_card.print(), "Gary Plotter (11) - achieved a grade of A+"); + assert_eq!( + report_card.print(), + "Gary Plotter (11) - achieved a grade of A+" + ); } -} \ No newline at end of file +}