Skip to content

Commit 5979d40

Browse files
feat: move generics3 to be quiz3
1 parent d61f795 commit 5979d40

File tree

3 files changed

+55
-92
lines changed

3 files changed

+55
-92
lines changed

exercises/generics/generics3.rs

-58
This file was deleted.

exercises/quiz3.rs

+45-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,62 @@
11
// 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!
410

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.
916

1017
// I AM NOT DONE
1118

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+
}
1430
}
1531

1632
#[cfg(test)]
1733
mod tests {
1834
use super::*;
1935

2036
#[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+
);
2347
}
2448

2549
#[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+
);
2961
}
3062
}

info.toml

+10-21
Original file line numberDiff line numberDiff line change
@@ -681,19 +681,6 @@ Maybe we could update the explicit references to this data type somehow?
681681
If you are still stuck https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-method-definitions
682682
"""
683683

684-
[[exercises]]
685-
name = "generics3"
686-
path = "exercises/generics/generics3.rs"
687-
mode = "test"
688-
hint = """
689-
To find the best solution to this challenge you're going to need to think back to your
690-
knowledge of traits, specifically Trait Bound Syntax - you may also need this: "use std::fmt::Display;"
691-
692-
This is definitely harder than the last two exercises! You need to think about not only making the
693-
ReportCard struct generic, but also the correct property - you will need to change the implementation
694-
of the struct slightly too...you can do it!
695-
"""
696-
697684
# TRAITS
698685

699686
[[exercises]]
@@ -716,6 +703,16 @@ Try mutating the incoming string vector.
716703
Vectors provide suitable methods for adding an element at the end. See
717704
the documentation at: https://doc.rust-lang.org/std/vec/struct.Vec.html"""
718705

706+
# QUIZ 3
707+
708+
[[exercises]]
709+
name = "quiz3"
710+
path = "exercises/quiz3.rs"
711+
mode = "test"
712+
hint = """
713+
To find the best solution to this challenge you're going to need to think back to your
714+
knowledge of traits, specifically Trait Bound Syntax - you may also need this: "use std::fmt::Display;""""
715+
719716
# TESTS
720717

721718
[[exercises]]
@@ -748,14 +745,6 @@ You can call a function right where you're passing arguments to `assert!` -- so
748745
something like `assert!(having_fun())`. If you want to check that you indeed get false, you
749746
can negate the result of what you're doing using `!`, like `assert!(!having_fun())`."""
750747

751-
# TEST 3
752-
753-
[[exercises]]
754-
name = "quiz3"
755-
path = "exercises/quiz3.rs"
756-
mode = "test"
757-
hint = "No hints this time ;)"
758-
759748
# STANDARD LIBRARY TYPES
760749

761750
[[exercises]]

0 commit comments

Comments
 (0)