|
1 | 1 | // quiz3.rs
|
2 |
| -// This is a quiz for the following sections: |
3 |
| -// - Tests |
| 2 | +// This quiz tests: |
| 3 | +// - Generics |
| 4 | +// - Traits |
| 5 | +// An imaginary magical school has a new report card generation system written in Rust! |
| 6 | +// Currently the system only supports creating report cards where the student's grade |
| 7 | +// is represented numerically (e.g. 1.0 -> 5.5). |
| 8 | +// However, the school also issues alphabetical grades (A+ -> F-) and needs |
| 9 | +// to be able to print both types of report card! |
4 | 10 |
|
5 |
| -// This quiz isn't testing our function -- make it do that in such a way that |
6 |
| -// the test passes. Then write a second test that tests that we get the result |
7 |
| -// we expect to get when we call `times_two` with a negative number. |
8 |
| -// No hints, you can do this :) |
| 11 | +// Make the necessary code changes in the struct ReportCard and the impl block |
| 12 | +// to support alphabetical report cards. Change the Grade in the second test to "A+" |
| 13 | +// to show that your changes allow alphabetical grades. |
| 14 | + |
| 15 | +// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint. |
9 | 16 |
|
10 | 17 | // I AM NOT DONE
|
11 | 18 |
|
12 |
| -pub fn times_two(num: i32) -> i32 { |
13 |
| - num * 2 |
| 19 | +pub struct ReportCard { |
| 20 | + pub grade: f32, |
| 21 | + pub student_name: String, |
| 22 | + pub student_age: u8, |
| 23 | +} |
| 24 | + |
| 25 | +impl ReportCard { |
| 26 | + pub fn print(&self) -> String { |
| 27 | + format!("{} ({}) - achieved a grade of {}", |
| 28 | + &self.student_name, &self.student_age, &self.grade) |
| 29 | + } |
14 | 30 | }
|
15 | 31 |
|
16 | 32 | #[cfg(test)]
|
17 | 33 | mod tests {
|
18 | 34 | use super::*;
|
19 | 35 |
|
20 | 36 | #[test]
|
21 |
| - fn returns_twice_of_positive_numbers() { |
22 |
| - assert_eq!(times_two(4), ???); |
| 37 | + fn generate_numeric_report_card() { |
| 38 | + let report_card = ReportCard { |
| 39 | + grade: 2.1, |
| 40 | + student_name: "Tom Wriggle".to_string(), |
| 41 | + student_age: 12, |
| 42 | + }; |
| 43 | + assert_eq!( |
| 44 | + report_card.print(), |
| 45 | + "Tom Wriggle (12) - achieved a grade of 2.1" |
| 46 | + ); |
23 | 47 | }
|
24 | 48 |
|
25 | 49 | #[test]
|
26 |
| - fn returns_twice_of_negative_numbers() { |
27 |
| - // TODO replace unimplemented!() with an assert for `times_two(-4)` |
28 |
| - unimplemented!() |
| 50 | + fn generate_alphabetic_report_card() { |
| 51 | + // TODO: Make sure to change the grade here after you finish the exercise. |
| 52 | + let report_card = ReportCard { |
| 53 | + grade: 2.1, |
| 54 | + student_name: "Gary Plotter".to_string(), |
| 55 | + student_age: 11, |
| 56 | + }; |
| 57 | + assert_eq!( |
| 58 | + report_card.print(), |
| 59 | + "Gary Plotter (11) - achieved a grade of A+" |
| 60 | + ); |
29 | 61 | }
|
30 | 62 | }
|
0 commit comments