Skip to content

Commit b135b58

Browse files
author
fmoko
authored
Merge pull request #280 from sjmann/generics-exercises
feat: added generics exercises
2 parents 9dc4040 + 0f8001e commit b135b58

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
4+
// I AM NOT DONE
5+
struct Wrapper<u32> {
6+
value: u32
7+
}
8+
9+
impl<u32> Wrapper<u32> {
10+
pub fn new(value: u32) -> Self {
11+
Wrapper { value }
12+
}
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
19+
#[test]
20+
fn store_u32_in_wrapper() {
21+
assert_eq!(Wrapper::new(42).value, 42);
22+
}
23+
24+
#[test]
25+
fn store_str_in_wrapper() {
26+
// TODO: Delete this assert and uncomment the one below once you have finished the exercise.
27+
assert!(false);
28+
// assert_eq!(Wrapper::new("Foo").value, "Foo");
29+
}
30+
}

exercises/generics/generics3.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
// I AM NOT DONE
10+
pub struct ReportCard {
11+
pub grade: f32,
12+
pub student_name: String,
13+
pub student_age: u8,
14+
}
15+
16+
impl ReportCard {
17+
pub fn print(&self) -> String {
18+
format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade)
19+
}
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
fn generate_numeric_report_card() {
28+
let report_card = ReportCard {
29+
grade: 2.1,
30+
student_name: "Tom Wriggle".to_string(),
31+
student_age: 12,
32+
};
33+
assert_eq!(report_card.print(), "Tom Wriggle (12) - achieved a grade of 2.1");
34+
}
35+
36+
#[test]
37+
fn generate_alphabetic_report_card() {
38+
// TODO: Make sure to change the grade here after you finish the exercise.
39+
let report_card = ReportCard {
40+
grade: 2.1,
41+
student_name: "Gary Plotter".to_string(),
42+
student_age: 11,
43+
};
44+
assert_eq!(report_card.print(), "Gary Plotter (11) - achieved a grade of A+");
45+
}
46+
}

info.toml

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

627+
# Generics
628+
629+
[[exercises]]
630+
name = "generics1"
631+
path = "exercises/generics/generics1.rs"
632+
mode = "compile"
633+
hint = """
634+
Vectors in rust make use of generics to create dynamically sized arrays of any type.
635+
You need to tell the compiler what type we are pushing onto this vector."""
636+
637+
[[exercises]]
638+
name = "generics2"
639+
path = "exercises/generics/generics2.rs"
640+
mode = "test"
641+
hint = """
642+
Think carefully about what we need to do here. Currently we are wrapping only values of
643+
type 'u32'. Maybe we need to update the explicit references to this data type somehow?
644+
"""
645+
646+
[[exercises]]
647+
name = "generics3"
648+
path = "exercises/generics/generics3.rs"
649+
mode = "test"
650+
hint = """
651+
To find the best solution to this challenge you're going to need to think back to your
652+
knowledge of traits, specifically Trait Bound Syntax - you may also need this: "use std::fmt::Display;"
653+
654+
This is definitely harder than the last two exercises! You need to think about not only making the
655+
ReportCard struct generic, but also the correct property - you will need to change the implementation
656+
of the struct slightly too...you can do it!
657+
"""
658+
627659
# THREADS
628660

629661
[[exercises]]

0 commit comments

Comments
 (0)