···1212// block to support alphabetical report cards in addition to numerical ones.
13131414// TODO: Adjust the struct as described above.
1515-struct ReportCard {
1616- grade: f32,
1515+struct ReportCard<T> {
1616+ grade: T,
1717 student_name: String,
1818 student_age: u8,
1919}
20202121// TODO: Adjust the impl block as described above.
2222-impl ReportCard {
2222+impl<T: std::fmt::Display> ReportCard<T> {
2323 fn print(&self) -> String {
2424 format!(
2525 "{} ({}) - achieved a grade of {}",
+63-2
solutions/quizzes/quiz3.rs
···11+// An imaginary magical school has a new report card generation system written
22+// in Rust! Currently, the system only supports creating report cards where the
33+// student's grade is represented numerically (e.g. 1.0 -> 5.5). However, the
44+// school also issues alphabetical grades (A+ -> F-) and needs to be able to
55+// print both types of report card!
66+//
77+// Make the necessary code changes in the struct `ReportCard` and the impl
88+// block to support alphabetical report cards in addition to numerical ones.
99+1010+use std::fmt::Display;
1111+1212+// Make the struct generic over `T`.
1313+struct ReportCard<T> {
1414+ // ^^^
1515+ grade: T,
1616+ // ^
1717+ student_name: String,
1818+ student_age: u8,
1919+}
2020+2121+// To be able to print the grade, it has to implement the `Display` trait.
2222+impl<T: Display> ReportCard<T> {
2323+ // ^^^^^^^ require that `T` implements `Display`.
2424+ fn print(&self) -> String {
2525+ format!(
2626+ "{} ({}) - achieved a grade of {}",
2727+ &self.student_name, &self.student_age, &self.grade,
2828+ )
2929+ }
3030+}
3131+132fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
3333+ // You can optionally experiment here.
3434+}
3535+3636+#[cfg(test)]
3737+mod tests {
3838+ use super::*;
3939+4040+ #[test]
4141+ fn generate_numeric_report_card() {
4242+ let report_card = ReportCard {
4343+ grade: 2.1,
4444+ student_name: "Tom Wriggle".to_string(),
4545+ student_age: 12,
4646+ };
4747+ assert_eq!(
4848+ report_card.print(),
4949+ "Tom Wriggle (12) - achieved a grade of 2.1",
5050+ );
5151+ }
5252+5353+ #[test]
5454+ fn generate_alphabetic_report_card() {
5555+ let report_card = ReportCard {
5656+ grade: "A+",
5757+ student_name: "Gary Plotter".to_string(),
5858+ student_age: 11,
5959+ };
6060+ assert_eq!(
6161+ report_card.print(),
6262+ "Gary Plotter (11) - achieved a grade of A+",
6363+ );
6464+ }
465}