Skip to content

Commit 76be5e4

Browse files
committed
feat: added new exercises for generics
1 parent f981dcf commit 76be5e4

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

exercises/generics/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Generics
2+
3+
In this section you'll learn about saving yourself many lines of code with generics!
4+
5+
### Book Sections
6+
7+
- [Generic Data Types](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html)

exercises/generics/generics1.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This shopping list program isn't compiling!
2+
// Use your knowledge of generics to fix it.
3+
4+
// I AM NOT DONE
5+
6+
fn main() {
7+
let mut shopping_list: Vec<?> = Vec::new();
8+
shopping_list.push("milk");
9+
}
10+

exercises/generics/generics2.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This powerful wrapper provides the ability to store a positive integer value.
2+
// Rewrite it using generics so that it supports wrapping ANY type.
3+
struct Wrapper<u32> {
4+
value: u32
5+
}
6+
7+
impl<u32> Wrapper<u32> {
8+
pub fn new(value: u32) -> Self {
9+
Wrapper { value }
10+
}
11+
}
12+
13+
#[cfg(test)]
14+
mod tests {
15+
use super::*;
16+
17+
#[test]
18+
fn store_u32_in_wrapper() {
19+
assert_eq!(Wrapper::new(42).value, 42);
20+
}
21+
22+
#[test]
23+
fn store_str_in_wrapper() {
24+
// TODO: Delete this assert and uncomment the one below once you have finished the exercise.
25+
assert!(false);
26+
// assert_eq!(Wrapper::new("Foo").value, "Foo");
27+
}
28+
}

exercises/generics/generics3.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// An imaginary magical school has a new report card generation system written in rust!
2+
// Currently the system only supports creating report cards where the student's grade
3+
// is represented numerically (e.g. 1.0 -> 5.5). However, the school also issues alphabetical grades
4+
// (A+ -> F-) and needs to be able to print both types of report card!
5+
6+
// Make the necessary code changes to support alphabetical report cards, thereby making the second
7+
// test pass.
8+
9+
pub struct ReportCard {
10+
pub grade: f32,
11+
pub student_name: String,
12+
pub student_age: u8,
13+
}
14+
15+
impl ReportCard {
16+
pub fn print(&self) -> String {
17+
format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade)
18+
}
19+
}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use super::*;
24+
25+
#[test]
26+
fn generate_numeric_report_card() {
27+
let report_card = ReportCard {
28+
grade: 2.1,
29+
student_name: "Tom Wriggle".to_string(),
30+
student_age: 12,
31+
};
32+
assert_eq!(report_card.print(), "Tom Wriggle (12) - achieved a grade of 2.1");
33+
}
34+
35+
#[test]
36+
fn generate_alphabetic_report_card() {
37+
// TODO: Make sure to change the grade here after you finish the exercise.
38+
let report_card = ReportCard {
39+
grade: 2.1,
40+
student_name: "Gary Plotter".to_string(),
41+
student_age: 11,
42+
};
43+
assert_eq!(report_card.print(), "Gary Plotter (11) - achieved a grade of A+");
44+
}
45+
}

info.toml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,38 @@ Try mutating the incoming string vector.
608608
Vectors provide suitable methods for adding an element at the end. See
609609
the documentation at: https://doc.rust-lang.org/std/vec/struct.Vec.html"""
610610

611+
# Generics
612+
613+
[[exercises]]
614+
name = "generics1"
615+
path = "exercises/generics/generics1.rs"
616+
mode = "compile"
617+
hint = """
618+
Vectors in rust make use of generics to create dynamically sized arrays of any type.
619+
You need to tell the compiler what type we are pushing onto this vector."""
620+
621+
[[exercises]]
622+
name = "generics2"
623+
path = "exercises/generics/generics2.rs"
624+
mode = "test"
625+
hint = """
626+
Think carefully about what we need to do here. Currently we are wrapping only values of
627+
type 'u32'. Maybe we need to update the explicit references to this data type somehow?
628+
"""
629+
630+
[[exercises]]
631+
name = "generics3"
632+
path = "exercises/generics/generics3_solution.rs"
633+
mode = "test"
634+
hint = """
635+
To find the best solution to this challenge you're going to need to think back to your
636+
knowledge of traits, specifically Trait Bound Syntax - you may also need this: "use std::fmt::Display;"
637+
638+
This is definitely harder than the last two exercises! You need to think about not only making the
639+
ReportCard struct generic, but also the correct property - you will need to change the implementation
640+
of the struct slightly too...you can do it!
641+
"""
642+
611643
# THREADS
612644

613645
[[exercises]]

0 commit comments

Comments
 (0)