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!
// I AM NOT DONE
fn main() {
let mut shopping_list: Vec<?> = Vec::new();
let mut shopping_list: Vec<_> = Vec::new();
shopping_list.push("milk");
}

View File

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

View File

@ -10,15 +10,18 @@
// Execute 'rustlings hint generics3' for hints!
// I AM NOT DONE
pub trait ReportCardGrade: std::fmt::Display {}
pub struct ReportCard {
pub grade: f32,
impl ReportCardGrade for f32 {}
impl ReportCardGrade for &str {}
pub struct ReportCard<T: ReportCardGrade> {
pub grade: T,
pub student_name: String,
pub student_age: u8,
}
impl ReportCard {
impl<T: ReportCardGrade> ReportCard<T> {
pub fn print(&self) -> String {
format!("{} ({}) - achieved a grade of {}",
&self.student_name, &self.student_age, &self.grade)
@ -46,7 +49,7 @@ mod tests {
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,
grade: "A+",
student_name: "Gary Plotter".to_string(),
student_age: 11,
};