feat: added new exercises for generics
This commit is contained in:
parent
f981dcfde4
commit
76be5e4e99
7
exercises/generics/README.md
Normal file
7
exercises/generics/README.md
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
### Generics
|
||||||
|
|
||||||
|
In this section you'll learn about saving yourself many lines of code with generics!
|
||||||
|
|
||||||
|
### Book Sections
|
||||||
|
|
||||||
|
- [Generic Data Types](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html)
|
10
exercises/generics/generics1.rs
Normal file
10
exercises/generics/generics1.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
// This shopping list program isn't compiling!
|
||||||
|
// Use your knowledge of generics to fix it.
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut shopping_list: Vec<?> = Vec::new();
|
||||||
|
shopping_list.push("milk");
|
||||||
|
}
|
||||||
|
|
28
exercises/generics/generics2.rs
Normal file
28
exercises/generics/generics2.rs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
// This powerful wrapper provides the ability to store a positive integer value.
|
||||||
|
// Rewrite it using generics so that it supports wrapping ANY type.
|
||||||
|
struct Wrapper<u32> {
|
||||||
|
value: u32
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<u32> Wrapper<u32> {
|
||||||
|
pub fn new(value: u32) -> Self {
|
||||||
|
Wrapper { value }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_u32_in_wrapper() {
|
||||||
|
assert_eq!(Wrapper::new(42).value, 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn store_str_in_wrapper() {
|
||||||
|
// TODO: Delete this assert and uncomment the one below once you have finished the exercise.
|
||||||
|
assert!(false);
|
||||||
|
// assert_eq!(Wrapper::new("Foo").value, "Foo");
|
||||||
|
}
|
||||||
|
}
|
45
exercises/generics/generics3.rs
Normal file
45
exercises/generics/generics3.rs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
// An imaginary magical school has a new report card generation system written in rust!
|
||||||
|
// Currently the system only supports creating report cards where the student's grade
|
||||||
|
// is represented numerically (e.g. 1.0 -> 5.5). However, the school also issues alphabetical grades
|
||||||
|
// (A+ -> F-) and needs to be able to print both types of report card!
|
||||||
|
|
||||||
|
// Make the necessary code changes to support alphabetical report cards, thereby making the second
|
||||||
|
// test pass.
|
||||||
|
|
||||||
|
pub struct ReportCard {
|
||||||
|
pub grade: f32,
|
||||||
|
pub student_name: String,
|
||||||
|
pub student_age: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ReportCard {
|
||||||
|
pub fn print(&self) -> String {
|
||||||
|
format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn generate_numeric_report_card() {
|
||||||
|
let report_card = ReportCard {
|
||||||
|
grade: 2.1,
|
||||||
|
student_name: "Tom Wriggle".to_string(),
|
||||||
|
student_age: 12,
|
||||||
|
};
|
||||||
|
assert_eq!(report_card.print(), "Tom Wriggle (12) - achieved a grade of 2.1");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn generate_alphabetic_report_card() {
|
||||||
|
// TODO: Make sure to change the grade here after you finish the exercise.
|
||||||
|
let report_card = ReportCard {
|
||||||
|
grade: 2.1,
|
||||||
|
student_name: "Gary Plotter".to_string(),
|
||||||
|
student_age: 11,
|
||||||
|
};
|
||||||
|
assert_eq!(report_card.print(), "Gary Plotter (11) - achieved a grade of A+");
|
||||||
|
}
|
||||||
|
}
|
32
info.toml
32
info.toml
|
@ -608,6 +608,38 @@ Try mutating the incoming string vector.
|
||||||
Vectors provide suitable methods for adding an element at the end. See
|
Vectors provide suitable methods for adding an element at the end. See
|
||||||
the documentation at: https://doc.rust-lang.org/std/vec/struct.Vec.html"""
|
the documentation at: https://doc.rust-lang.org/std/vec/struct.Vec.html"""
|
||||||
|
|
||||||
|
# Generics
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "generics1"
|
||||||
|
path = "exercises/generics/generics1.rs"
|
||||||
|
mode = "compile"
|
||||||
|
hint = """
|
||||||
|
Vectors in rust make use of generics to create dynamically sized arrays of any type.
|
||||||
|
You need to tell the compiler what type we are pushing onto this vector."""
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "generics2"
|
||||||
|
path = "exercises/generics/generics2.rs"
|
||||||
|
mode = "test"
|
||||||
|
hint = """
|
||||||
|
Think carefully about what we need to do here. Currently we are wrapping only values of
|
||||||
|
type 'u32'. Maybe we need to update the explicit references to this data type somehow?
|
||||||
|
"""
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "generics3"
|
||||||
|
path = "exercises/generics/generics3_solution.rs"
|
||||||
|
mode = "test"
|
||||||
|
hint = """
|
||||||
|
To find the best solution to this challenge you're going to need to think back to your
|
||||||
|
knowledge of traits, specifically Trait Bound Syntax - you may also need this: "use std::fmt::Display;"
|
||||||
|
|
||||||
|
This is definitely harder than the last two exercises! You need to think about not only making the
|
||||||
|
ReportCard struct generic, but also the correct property - you will need to change the implementation
|
||||||
|
of the struct slightly too...you can do it!
|
||||||
|
"""
|
||||||
|
|
||||||
# THREADS
|
# THREADS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
|
Loading…
Reference in a new issue