feat: 🎸 added solution for quiz3

This commit is contained in:
Karan Kadam 2022-12-25 16:30:42 +10:30
parent 913ba451fd
commit 6adcb5400b

View File

@ -14,18 +14,38 @@
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE pub trait Grade {
fn to_string(&self) -> String;
}
pub struct ReportCard { pub struct StrGrade(String);
pub grade: f32, pub struct NumGrade(f32);
impl Grade for StrGrade {
fn to_string(&self) -> String {
self.0.to_string()
}
}
impl Grade for NumGrade {
fn to_string(&self) -> String {
self.0.to_string()
}
}
pub struct ReportCard<G: Grade> {
pub grade: G,
pub student_name: String, pub student_name: String,
pub student_age: u8, pub student_age: u8,
} }
impl ReportCard { impl<G: Grade> ReportCard<G> {
pub fn print(&self) -> String { pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {}", format!(
&self.student_name, &self.student_age, &self.grade) "{} ({}) - achieved a grade of {}",
&self.student_name,
&self.student_age,
&self.grade.to_string()
)
} }
} }
@ -36,7 +56,7 @@ mod tests {
#[test] #[test]
fn generate_numeric_report_card() { fn generate_numeric_report_card() {
let report_card = ReportCard { let report_card = ReportCard {
grade: 2.1, grade: NumGrade(2.1),
student_name: "Tom Wriggle".to_string(), student_name: "Tom Wriggle".to_string(),
student_age: 12, student_age: 12,
}; };
@ -50,7 +70,7 @@ mod tests {
fn generate_alphabetic_report_card() { fn generate_alphabetic_report_card() {
// TODO: Make sure to change the grade here after you finish the exercise. // TODO: Make sure to change the grade here after you finish the exercise.
let report_card = ReportCard { let report_card = ReportCard {
grade: 2.1, grade: StrGrade(String::from("A+")),
student_name: "Gary Plotter".to_string(), student_name: "Gary Plotter".to_string(),
student_age: 11, student_age: 11,
}; };