Skip to content

Commit 66f33ff

Browse files
authored
Update quiz3.rs
1 parent 0e217df commit 66f33ff

File tree

1 file changed

+7
-25
lines changed

1 file changed

+7
-25
lines changed

exercises/quiz3.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
1-
// quiz3.rs
2-
//
3-
// This quiz tests:
4-
// - Generics
5-
// - Traits
6-
//
7-
// An imaginary magical school has a new report card generation system written
8-
// in Rust! Currently the system only supports creating report cards where the
9-
// student's grade is represented numerically (e.g. 1.0 -> 5.5). However, the
10-
// school also issues alphabetical grades (A+ -> F-) and needs to be able to
11-
// print both types of report card!
12-
//
13-
// Make the necessary code changes in the struct ReportCard and the impl block
14-
// to support alphabetical report cards. Change the Grade in the second test to
15-
// "A+" to show that your changes allow alphabetical grades.
16-
//
17-
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.
18-
19-
// I AM NOT DONE
20-
21-
pub struct ReportCard {
22-
pub grade: f32,
1+
// 为ReportCard添加泛型,支持任意可格式化的成绩类型
2+
pub struct ReportCard<G> {
3+
pub grade: G,
234
pub student_name: String,
245
pub student_age: u8,
256
}
267

27-
impl ReportCard {
8+
impl<G: std::fmt::Display> ReportCard<G> {
289
pub fn print(&self) -> String {
2910
format!("{} ({}) - achieved a grade of {}",
3011
&self.student_name, &self.student_age, &self.grade)
@@ -37,6 +18,7 @@ mod tests {
3718

3819
#[test]
3920
fn generate_numeric_report_card() {
21+
// 泛型参数G为f32类型
4022
let report_card = ReportCard {
4123
grade: 2.1,
4224
student_name: "Tom Wriggle".to_string(),
@@ -50,9 +32,9 @@ mod tests {
5032

5133
#[test]
5234
fn generate_alphabetic_report_card() {
53-
// TODO: Make sure to change the grade here after you finish the exercise.
35+
// 泛型参数G为&str类型(也可使用String)
5436
let report_card = ReportCard {
55-
grade: 2.1,
37+
grade: "A+", // 修改为字母成绩
5638
student_name: "Gary Plotter".to_string(),
5739
student_age: 11,
5840
};

0 commit comments

Comments
 (0)