finish generics

This commit is contained in:
NoneTheWisr 2022-01-28 18:39:18 +04:00
parent c4e1ea89f9
commit 652d862092
3 changed files with 13 additions and 14 deletions

View File

@ -3,9 +3,7 @@
// Execute `rustlings hint generics1` for hints! // Execute `rustlings hint generics1` for hints!
// I AM NOT DONE
fn main() { fn main() {
let mut shopping_list: Vec<?> = Vec::new(); let mut shopping_list: Vec<_> = Vec::new();
shopping_list.push("milk"); shopping_list.push("milk");
} }

View File

@ -3,14 +3,12 @@
// Execute `rustlings hint generics2` for hints! // Execute `rustlings hint generics2` for hints!
// I AM NOT DONE struct Wrapper<T> {
value: T,
struct Wrapper {
value: u32,
} }
impl Wrapper { impl<T> Wrapper<T> {
pub fn new(value: u32) -> Self { pub fn new(value: T) -> Self {
Wrapper { value } Wrapper { value }
} }
} }

View File

@ -10,15 +10,18 @@
// Execute 'rustlings hint generics3' for hints! // Execute 'rustlings hint generics3' for hints!
// I AM NOT DONE pub trait ReportCardGrade: std::fmt::Display {}
pub struct ReportCard { impl ReportCardGrade for f32 {}
pub grade: f32, impl ReportCardGrade for &str {}
pub struct ReportCard<T: ReportCardGrade> {
pub grade: T,
pub student_name: String, pub student_name: String,
pub student_age: u8, pub student_age: u8,
} }
impl ReportCard { impl<T: ReportCardGrade> ReportCard<T> {
pub fn print(&self) -> String { pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {}", format!("{} ({}) - achieved a grade of {}",
&self.student_name, &self.student_age, &self.grade) &self.student_name, &self.student_age, &self.grade)
@ -46,7 +49,7 @@ mod tests {
fn generate_alphabetic_report_card() { fn generate_alphabetic_report_card() {
// TODO: Make sure to change the grade here after you finish the exercise. // TODO: Make sure to change the grade here after you finish the exercise.
let report_card = ReportCard { let report_card = ReportCard {
grade: 2.1, grade: "A+",
student_name: "Gary Plotter".to_string(), student_name: "Gary Plotter".to_string(),
student_age: 11, student_age: 11,
}; };