-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added new exercises for generics
- Loading branch information
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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+"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters