From e86f633b6c8c5509c2db2ec580a8835b661a51ec Mon Sep 17 00:00:00 2001 From: MatyTan <1396568121@qq.com> Date: Tue, 22 Nov 2022 08:29:14 +0800 Subject: [PATCH] day7 --- exercises/error_handling/errors6.rs | 2 -- exercises/generics/generics1.rs | 4 +--- exercises/generics/generics2.rs | 10 ++++------ exercises/lifetimes/lifetimes1.rs | 4 +--- exercises/lifetimes/lifetimes2.rs | 11 +++++------ exercises/lifetimes/lifetimes3.rs | 13 +++++++------ exercises/quiz3.rs | 23 +++++++++++++++-------- exercises/tests/tests1.rs | 6 +++--- exercises/tests/tests2.rs | 6 +++--- exercises/tests/tests3.rs | 6 ++---- exercises/traits/traits1.rs | 7 +++++-- exercises/traits/traits2.rs | 9 +++++++-- exercises/traits/traits3.rs | 6 +++--- exercises/traits/traits4.rs | 4 +--- exercises/traits/traits5.rs | 5 +---- 15 files changed, 58 insertions(+), 58 deletions(-) diff --git a/exercises/error_handling/errors6.rs b/exercises/error_handling/errors6.rs index 7afac235..f1ba3ab7 100644 --- a/exercises/error_handling/errors6.rs +++ b/exercises/error_handling/errors6.rs @@ -8,8 +8,6 @@ // Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::num::ParseIntError; // This is a custom error type that we will be using in `parse_pos_nonzero()`. diff --git a/exercises/generics/generics1.rs b/exercises/generics/generics1.rs index 4c34ae47..e3ec91aa 100644 --- a/exercises/generics/generics1.rs +++ b/exercises/generics/generics1.rs @@ -3,9 +3,7 @@ // Execute `rustlings hint generics1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { - let mut shopping_list: Vec = Vec::new(); + let mut shopping_list: Vec<&str> = Vec::new(); shopping_list.push("milk"); } diff --git a/exercises/generics/generics2.rs b/exercises/generics/generics2.rs index aedbd55c..ef084440 100644 --- a/exercises/generics/generics2.rs +++ b/exercises/generics/generics2.rs @@ -3,14 +3,12 @@ // Execute `rustlings hint generics2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - -struct Wrapper { - value: u32, +struct Wrapper { + value: T, } -impl Wrapper { - pub fn new(value: u32) -> Self { +impl Wrapper { + pub fn new(value: T) -> Self { Wrapper { value } } } diff --git a/exercises/lifetimes/lifetimes1.rs b/exercises/lifetimes/lifetimes1.rs index 0236470d..a12d3f5b 100644 --- a/exercises/lifetimes/lifetimes1.rs +++ b/exercises/lifetimes/lifetimes1.rs @@ -7,9 +7,7 @@ // // Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - -fn longest(x: &str, y: &str) -> &str { +fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { diff --git a/exercises/lifetimes/lifetimes2.rs b/exercises/lifetimes/lifetimes2.rs index b48feabc..8226f049 100644 --- a/exercises/lifetimes/lifetimes2.rs +++ b/exercises/lifetimes/lifetimes2.rs @@ -6,8 +6,6 @@ // // Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x @@ -17,11 +15,12 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { } fn main() { - let string1 = String::from("long string is long"); - let result; + let string1 = String::from("long string is long"); //'a { - let string2 = String::from("xyz"); + //这意味着返回值的生命周期与参数生命周期中的较小值一致: + let result; //返回值引用的生命周期,应该小于等于x,y 里面最短的那个 + let string2 = String::from("xyz"); //'b result = longest(string1.as_str(), string2.as_str()); + println!("The longest string is '{}'", result); } - println!("The longest string is '{}'", result); } diff --git a/exercises/lifetimes/lifetimes3.rs b/exercises/lifetimes/lifetimes3.rs index ea483708..d18c8a61 100644 --- a/exercises/lifetimes/lifetimes3.rs +++ b/exercises/lifetimes/lifetimes3.rs @@ -4,17 +4,18 @@ // // Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - -struct Book { - author: &str, - title: &str, +struct Book<'a> { + author: &'a str, + title: &'a str, } fn main() { let name = String::from("Jill Smith"); let title = String::from("Fish Flying"); - let book = Book { author: &name, title: &title }; + let book = Book { + author: name.as_str(), + title: title.as_str(), + }; println!("{} by {}", book.title, book.author); } diff --git a/exercises/quiz3.rs b/exercises/quiz3.rs index 15dc4699..c38b1044 100644 --- a/exercises/quiz3.rs +++ b/exercises/quiz3.rs @@ -14,18 +14,25 @@ // Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - -pub struct ReportCard { - pub grade: f32, +pub struct ReportCard { + pub grade: T, pub student_name: String, pub student_age: u8, } -impl ReportCard { +pub trait Printable { + fn print(&self) -> String; +} + +impl ReportCard +where + T: std::fmt::Display, +{ pub fn print(&self) -> String { - format!("{} ({}) - achieved a grade of {}", - &self.student_name, &self.student_age, &self.grade) + format!( + "{} ({}) - achieved a grade of {}", + &self.student_name, &self.student_age, &self.grade + ) } } @@ -50,7 +57,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, }; diff --git a/exercises/tests/tests1.rs b/exercises/tests/tests1.rs index 8b6ea374..5a479036 100644 --- a/exercises/tests/tests1.rs +++ b/exercises/tests/tests1.rs @@ -7,12 +7,12 @@ // pass! Make the test fail! // Execute `rustlings hint tests1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[cfg(test)] mod tests { #[test] fn you_can_assert() { - assert!(); + let a = 10; + let b = 10; + assert!(a == b); } } diff --git a/exercises/tests/tests2.rs b/exercises/tests/tests2.rs index a5ac15b1..93459fb7 100644 --- a/exercises/tests/tests2.rs +++ b/exercises/tests/tests2.rs @@ -3,12 +3,12 @@ // pass! Make the test fail! // Execute `rustlings hint tests2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[cfg(test)] mod tests { #[test] fn you_can_assert_eq() { - assert_eq!(); + let a = 10; + let b = 10; + assert_eq!(a, b); } } diff --git a/exercises/tests/tests3.rs b/exercises/tests/tests3.rs index 196a81a0..74083e6c 100644 --- a/exercises/tests/tests3.rs +++ b/exercises/tests/tests3.rs @@ -4,8 +4,6 @@ // we expect to get when we call `is_even(5)`. // Execute `rustlings hint tests3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - pub fn is_even(num: i32) -> bool { num % 2 == 0 } @@ -16,11 +14,11 @@ mod tests { #[test] fn is_true_when_even() { - assert!(); + assert!(is_even(2)); } #[test] fn is_false_when_odd() { - assert!(); + assert!(!is_even(3)); } } diff --git a/exercises/traits/traits1.rs b/exercises/traits/traits1.rs index 5b9d8d50..12ee3500 100644 --- a/exercises/traits/traits1.rs +++ b/exercises/traits/traits1.rs @@ -9,14 +9,17 @@ // implementing this trait. // Execute `rustlings hint traits1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - trait AppendBar { fn append_bar(self) -> Self; } impl AppendBar for String { //Add your code here + fn append_bar(self) -> Self { + let mut s = self; + s.push_str("Bar"); + s + } } fn main() { diff --git a/exercises/traits/traits2.rs b/exercises/traits/traits2.rs index 708bb19a..09da462b 100644 --- a/exercises/traits/traits2.rs +++ b/exercises/traits/traits2.rs @@ -11,13 +11,18 @@ // you can do this! // Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - trait AppendBar { fn append_bar(self) -> Self; } //TODO: Add your code here +impl AppendBar for Vec { + fn append_bar(self) -> Self { + let mut v = self; + v.push("Bar".to_string()); + v + } +} #[cfg(test)] mod tests { diff --git a/exercises/traits/traits3.rs b/exercises/traits/traits3.rs index 6d2fd6c3..86e425fa 100644 --- a/exercises/traits/traits3.rs +++ b/exercises/traits/traits3.rs @@ -7,10 +7,10 @@ // Consider what you can add to the Licensed trait. // Execute `rustlings hint traits3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - pub trait Licensed { - fn licensing_info(&self) -> String; + fn licensing_info(&self) -> String { + String::from("Some information") + } } struct SomeSoftware { diff --git a/exercises/traits/traits4.rs b/exercises/traits/traits4.rs index 6b541665..6e829787 100644 --- a/exercises/traits/traits4.rs +++ b/exercises/traits/traits4.rs @@ -4,8 +4,6 @@ // Don't change any line other than the marked one. // Execute `rustlings hint traits4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - pub trait Licensed { fn licensing_info(&self) -> String { "some information".to_string() @@ -20,7 +18,7 @@ impl Licensed for SomeSoftware {} impl Licensed for OtherSoftware {} // YOU MAY ONLY CHANGE THE NEXT LINE -fn compare_license_types(software: ??, software_two: ??) -> bool { +fn compare_license_types(software: &impl Licensed, software_two: &impl Licensed) -> bool { software.licensing_info() == software_two.licensing_info() } diff --git a/exercises/traits/traits5.rs b/exercises/traits/traits5.rs index 0fbca28a..d46ea49b 100644 --- a/exercises/traits/traits5.rs +++ b/exercises/traits/traits5.rs @@ -1,11 +1,8 @@ // traits5.rs -// // Your task is to replace the '??' sections so the code compiles. // Don't change any line other than the marked one. // Execute `rustlings hint traits5` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - pub trait SomeTrait { fn some_function(&self) -> bool { true @@ -27,7 +24,7 @@ impl SomeTrait for OtherStruct {} impl OtherTrait for OtherStruct {} // YOU MAY ONLY CHANGE THE NEXT LINE -fn some_func(item: ??) -> bool { +fn some_func(item: (impl SomeTrait + OtherTrait)) -> bool { item.some_function() && item.other_function() }