diff --git a/exercises/14_generics/generics1.rs b/exercises/14_generics/generics1.rs index 35c1d2fe..f0062fe5 100644 --- a/exercises/14_generics/generics1.rs +++ b/exercises/14_generics/generics1.rs @@ -6,9 +6,8 @@ // 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/14_generics/generics2.rs b/exercises/14_generics/generics2.rs index 074cd938..e43b2a71 100644 --- a/exercises/14_generics/generics2.rs +++ b/exercises/14_generics/generics2.rs @@ -6,14 +6,13 @@ // 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/15_traits/traits1.rs b/exercises/15_traits/traits1.rs index 37dfcbfe..899e1625 100644 --- a/exercises/15_traits/traits1.rs +++ b/exercises/15_traits/traits1.rs @@ -7,7 +7,7 @@ // Execute `rustlings hint traits1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + trait AppendBar { fn append_bar(self) -> Self; @@ -15,6 +15,9 @@ trait AppendBar { impl AppendBar for String { // TODO: Implement `AppendBar` for type `String`. + fn append_bar(self)->Self{ + format!("{}Bar",self) + } } fn main() { diff --git a/exercises/15_traits/traits2.rs b/exercises/15_traits/traits2.rs index 3e35f8e1..4b0e974b 100644 --- a/exercises/15_traits/traits2.rs +++ b/exercises/15_traits/traits2.rs @@ -8,11 +8,16 @@ // // Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE trait AppendBar { fn append_bar(self) -> Self; } +impl AppendBar for Vec { + fn append_bar(mut self)->Self{ + self.push("Bar".to_string()); + self + } +} // TODO: Implement trait `AppendBar` for a vector of strings. diff --git a/exercises/15_traits/traits3.rs b/exercises/15_traits/traits3.rs index 4e2b06b0..f1aca263 100644 --- a/exercises/15_traits/traits3.rs +++ b/exercises/15_traits/traits3.rs @@ -8,10 +8,11 @@ // 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{ + "Some information".to_string() + } } struct SomeSoftware { diff --git a/exercises/15_traits/traits4.rs b/exercises/15_traits/traits4.rs index 4bda3e57..fa20b75a 100644 --- a/exercises/15_traits/traits4.rs +++ b/exercises/15_traits/traits4.rs @@ -7,7 +7,7 @@ // 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 { @@ -23,7 +23,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/15_traits/traits5.rs b/exercises/15_traits/traits5.rs index df183805..189e93ae 100644 --- a/exercises/15_traits/traits5.rs +++ b/exercises/15_traits/traits5.rs @@ -7,7 +7,6 @@ // 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 { @@ -30,7 +29,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() } diff --git a/exercises/16_lifetimes/lifetimes1.rs b/exercises/16_lifetimes/lifetimes1.rs index 87bde490..b40fc1c9 100644 --- a/exercises/16_lifetimes/lifetimes1.rs +++ b/exercises/16_lifetimes/lifetimes1.rs @@ -8,9 +8,9 @@ // 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/16_lifetimes/lifetimes2.rs b/exercises/16_lifetimes/lifetimes2.rs index 4f3d8c18..464d5b4f 100644 --- a/exercises/16_lifetimes/lifetimes2.rs +++ b/exercises/16_lifetimes/lifetimes2.rs @@ -6,7 +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() { @@ -22,6 +21,8 @@ fn main() { { let string2 = String::from("xyz"); result = longest(string1.as_str(), string2.as_str()); + //移动打印行 + println!("The longest string is '{}'", result); } - println!("The longest string is '{}'", result); + } diff --git a/exercises/16_lifetimes/lifetimes3.rs b/exercises/16_lifetimes/lifetimes3.rs index 9c59f9c0..7803e6cd 100644 --- a/exercises/16_lifetimes/lifetimes3.rs +++ b/exercises/16_lifetimes/lifetimes3.rs @@ -5,11 +5,11 @@ // 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() { diff --git a/exercises/17_tests/tests1.rs b/exercises/17_tests/tests1.rs index 810277ac..cfba0109 100644 --- a/exercises/17_tests/tests1.rs +++ b/exercises/17_tests/tests1.rs @@ -10,12 +10,13 @@ // 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!(); + // assert!(false); + assert!(true); } } diff --git a/exercises/17_tests/tests2.rs b/exercises/17_tests/tests2.rs index f8024e9f..bb8158da 100644 --- a/exercises/17_tests/tests2.rs +++ b/exercises/17_tests/tests2.rs @@ -6,12 +6,14 @@ // 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!(); + // assert_eq!(true,false); + assert_eq!(true,true); + assert_eq!("a","a"); } } diff --git a/exercises/17_tests/tests3.rs b/exercises/17_tests/tests3.rs index 4013e384..6d2ada49 100644 --- a/exercises/17_tests/tests3.rs +++ b/exercises/17_tests/tests3.rs @@ -7,7 +7,7 @@ // 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 @@ -19,11 +19,11 @@ mod tests { #[test] fn is_true_when_even() { - assert!(); + assert!(is_even(10)); } #[test] fn is_false_when_odd() { - assert!(); + assert!(!is_even(11)); } } diff --git a/exercises/17_tests/tests4.rs b/exercises/17_tests/tests4.rs index 935d0db1..a5e5b9c0 100644 --- a/exercises/17_tests/tests4.rs +++ b/exercises/17_tests/tests4.rs @@ -5,7 +5,7 @@ // Execute `rustlings hint tests4` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + struct Rectangle { width: i32, @@ -30,17 +30,20 @@ mod tests { fn correct_width_and_height() { // This test should check if the rectangle is the size that we pass into its constructor let rect = Rectangle::new(10, 20); - assert_eq!(???, 10); // check width - assert_eq!(???, 20); // check height + assert_eq!(rect.width, 10); // check width + assert_eq!(rect.height, 20); // check height } #[test] + #[should_panic] fn negative_width() { // This test should check if program panics when we try to create rectangle with negative width let _rect = Rectangle::new(-10, 10); + } #[test] + #[should_panic] fn negative_height() { // This test should check if program panics when we try to create rectangle with negative height let _rect = Rectangle::new(10, -10); diff --git a/exercises/18_iterators/iterators1.rs b/exercises/18_iterators/iterators1.rs index 31076bb9..6d84c0af 100644 --- a/exercises/18_iterators/iterators1.rs +++ b/exercises/18_iterators/iterators1.rs @@ -9,18 +9,17 @@ // Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE #[test] fn main() { let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"]; - let mut my_iterable_fav_fruits = ???; // TODO: Step 1 + let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1 assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2 + assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2 assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3 + assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3 assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4 + assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4 } diff --git a/exercises/18_iterators/iterators2.rs b/exercises/18_iterators/iterators2.rs index dda82a08..2a743432 100644 --- a/exercises/18_iterators/iterators2.rs +++ b/exercises/18_iterators/iterators2.rs @@ -6,7 +6,7 @@ // Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + // Step 1. // Complete the `capitalize_first` function. @@ -15,7 +15,7 @@ pub fn capitalize_first(input: &str) -> String { let mut c = input.chars(); match c.next() { None => String::new(), - Some(first) => ???, + Some(first) => first.to_uppercase().collect::()+c.as_str(), } } @@ -24,7 +24,8 @@ pub fn capitalize_first(input: &str) -> String { // Return a vector of strings. // ["hello", "world"] -> ["Hello", "World"] pub fn capitalize_words_vector(words: &[&str]) -> Vec { - vec![] + words.iter().map(|v|{capitalize_first(v)}).collect::>() + } // Step 3. @@ -32,7 +33,11 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec { // Return a single string. // ["hello", " ", "world"] -> "Hello World" pub fn capitalize_words_string(words: &[&str]) -> String { - String::new() + + words.iter() + .map(|&word| capitalize_first(word)) + .collect::>() + .join("") } #[cfg(test)] diff --git a/exercises/18_iterators/iterators3.rs b/exercises/18_iterators/iterators3.rs index 29fa23a3..180679aa 100644 --- a/exercises/18_iterators/iterators3.rs +++ b/exercises/18_iterators/iterators3.rs @@ -9,7 +9,6 @@ // Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE #[derive(Debug, PartialEq, Eq)] pub enum DivisionError { @@ -26,23 +25,37 @@ pub struct NotDivisibleError { // Calculate `a` divided by `b` if `a` is evenly divisible by `b`. // Otherwise, return a suitable error. pub fn divide(a: i32, b: i32) -> Result { - todo!(); + // todo!() + if b ==0 { + return Err(DivisionError::DivideByZero) + } + if a%b == 0{ + return Ok(a/b) + }else{ + return Err(DivisionError::NotDivisible(NotDivisibleError{ dividend: a,divisor: b})) + } } // Complete the function and return a value of the correct type so the test // passes. // Desired output: Ok([1, 11, 1426, 3]) -fn result_with_list() -> () { +fn result_with_list() -> Result, DivisionError>{ let numbers = vec![27, 297, 38502, 81]; - let division_results = numbers.into_iter().map(|n| divide(n, 27)); + let division_results: Result, DivisionError> = numbers.into_iter() + .map(|n| divide(n, 27)) + .collect(); + division_results } // Complete the function and return a value of the correct type so the test // passes. // Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)] -fn list_of_results() -> () { +fn list_of_results() -> Vec> { let numbers = vec![27, 297, 38502, 81]; - let division_results = numbers.into_iter().map(|n| divide(n, 27)); + let division_results: Vec> = numbers.into_iter() + .map(|n| divide(n, 27)) + .collect(); + division_results } #[cfg(test)] diff --git a/exercises/18_iterators/iterators4.rs b/exercises/18_iterators/iterators4.rs index 79e1692b..9aba53f7 100644 --- a/exercises/18_iterators/iterators4.rs +++ b/exercises/18_iterators/iterators4.rs @@ -3,9 +3,11 @@ // Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + pub fn factorial(num: u64) -> u64 { + // (1..=num).fold(1,|acc,x|acc*x) + (1..=num).product() // Complete this function to return the factorial of num // Do not use: // - return diff --git a/exercises/18_iterators/iterators5.rs b/exercises/18_iterators/iterators5.rs index a062ee4c..81d9137a 100644 --- a/exercises/18_iterators/iterators5.rs +++ b/exercises/18_iterators/iterators5.rs @@ -11,7 +11,6 @@ // Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE use std::collections::HashMap; @@ -35,7 +34,7 @@ fn count_for(map: &HashMap, value: Progress) -> usize { fn count_iterator(map: &HashMap, value: Progress) -> usize { // map is a hashmap with String keys and Progress values. // map = { "variables1": Complete, "from_str": None, ... } - todo!(); + map.values().filter(|&val|val == & value).count() } fn count_collection_for(collection: &[HashMap], value: Progress) -> usize { @@ -54,7 +53,10 @@ fn count_collection_iterator(collection: &[HashMap], value: Pr // collection is a slice of hashmaps. // collection = [{ "variables1": Complete, "from_str": None, ... }, // { "variables2": Complete, ... }, ... ] - todo!(); + collection.iter().map(|map|{ + map.values().filter(|&val|val == & value).count() + }).sum() + } #[cfg(test)] diff --git a/exercises/19_smart_pointers/arc1.rs b/exercises/19_smart_pointers/arc1.rs index 3526ddcb..982d0c3c 100644 --- a/exercises/19_smart_pointers/arc1.rs +++ b/exercises/19_smart_pointers/arc1.rs @@ -21,7 +21,7 @@ // // Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE + #![forbid(unused_imports)] // Do not change this, (or the next) line. use std::sync::Arc; @@ -29,11 +29,11 @@ use std::thread; fn main() { let numbers: Vec<_> = (0..100u32).collect(); - let shared_numbers = // TODO + let shared_numbers = Arc::new(numbers); // TODO let mut joinhandles = Vec::new(); for offset in 0..8 { - let child_numbers = // TODO + let child_numbers =Arc::clone(&shared_numbers); // TODO joinhandles.push(thread::spawn(move || { let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum(); println!("Sum of offset {} is {}", offset, sum); diff --git a/exercises/19_smart_pointers/box1.rs b/exercises/19_smart_pointers/box1.rs index 513e7daa..9def2bc9 100644 --- a/exercises/19_smart_pointers/box1.rs +++ b/exercises/19_smart_pointers/box1.rs @@ -18,11 +18,10 @@ // // Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE #[derive(PartialEq, Debug)] pub enum List { - Cons(i32, List), + Cons(i32, Box), Nil, } @@ -35,11 +34,13 @@ fn main() { } pub fn create_empty_list() -> List { - todo!() + List::Nil + } +//创建不为空的 pub fn create_non_empty_list() -> List { - todo!() + List::Cons(0,Box::new(List::Nil)) } #[cfg(test)] diff --git a/exercises/19_smart_pointers/cow1.rs b/exercises/19_smart_pointers/cow1.rs index fcd3e0bb..0c27aafd 100644 --- a/exercises/19_smart_pointers/cow1.rs +++ b/exercises/19_smart_pointers/cow1.rs @@ -12,7 +12,7 @@ // // Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE + use std::borrow::Cow; @@ -49,6 +49,8 @@ mod tests { let mut input = Cow::from(&slice[..]); match abs_all(&mut input) { // TODO + Cow::Borrowed(_)=>{Ok(())} + _ => Err("Expected Borrowed value"), } } @@ -61,6 +63,8 @@ mod tests { let mut input = Cow::from(slice); match abs_all(&mut input) { // TODO + Cow::Owned(_) => Ok(()), + _ => Err("Expected owned value"), } } @@ -73,6 +77,8 @@ mod tests { let mut input = Cow::from(slice); match abs_all(&mut input) { // TODO + Cow::Owned(_) => Ok(()), + _ => Err("Expected owned value"), } } } diff --git a/exercises/19_smart_pointers/rc1.rs b/exercises/19_smart_pointers/rc1.rs index 1b903469..1265a5c9 100644 --- a/exercises/19_smart_pointers/rc1.rs +++ b/exercises/19_smart_pointers/rc1.rs @@ -10,7 +10,6 @@ // // Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE use std::rc::Rc; @@ -61,17 +60,17 @@ fn main() { jupiter.details(); // TODO - let saturn = Planet::Saturn(Rc::new(Sun {})); + let saturn = Planet::Saturn(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 7 references saturn.details(); // TODO - let uranus = Planet::Uranus(Rc::new(Sun {})); + let uranus = Planet::Uranus(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 8 references uranus.details(); // TODO - let neptune = Planet::Neptune(Rc::new(Sun {})); + let neptune = Planet::Neptune(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 9 references neptune.details(); @@ -91,13 +90,13 @@ fn main() { drop(mars); println!("reference count = {}", Rc::strong_count(&sun)); // 4 references - + drop(earth); // TODO println!("reference count = {}", Rc::strong_count(&sun)); // 3 references - + drop(venus); // TODO println!("reference count = {}", Rc::strong_count(&sun)); // 2 references - + drop(mercury); // TODO println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference diff --git a/exercises/20_threads/threads1.rs b/exercises/20_threads/threads1.rs index 80b6def3..df6ca355 100644 --- a/exercises/20_threads/threads1.rs +++ b/exercises/20_threads/threads1.rs @@ -8,7 +8,6 @@ // Execute `rustlings hint threads1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE use std::thread; use std::time::{Duration, Instant}; @@ -27,6 +26,7 @@ fn main() { let mut results: Vec = vec![]; for handle in handles { // TODO: a struct is returned from thread::spawn, can you use it? + results.push(handle.join().unwrap()) } if results.len() != 10 { diff --git a/exercises/20_threads/threads2.rs b/exercises/20_threads/threads2.rs index 62dad80d..43531057 100644 --- a/exercises/20_threads/threads2.rs +++ b/exercises/20_threads/threads2.rs @@ -7,25 +7,26 @@ // Execute `rustlings hint threads2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE use std::sync::Arc; use std::thread; use std::time::Duration; +use std::sync::Mutex; struct JobStatus { jobs_completed: u32, } fn main() { - let status = Arc::new(JobStatus { jobs_completed: 0 }); + let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 })); let mut handles = vec![]; for _ in 0..10 { let status_shared = Arc::clone(&status); let handle = thread::spawn(move || { thread::sleep(Duration::from_millis(250)); // TODO: You must take an action before you update a shared value - status_shared.jobs_completed += 1; + let mut status_shared_mut= status_shared.lock().unwrap(); + status_shared_mut.jobs_completed += 1; }); handles.push(handle); } @@ -34,6 +35,7 @@ fn main() { // TODO: Print the value of the JobStatus.jobs_completed. Did you notice // anything interesting in the output? Do you have to 'join' on all the // handles? - println!("jobs completed {}", ???); + let status = status.lock().unwrap(); + println!("jobs completed {}", status.jobs_completed); } } diff --git a/exercises/20_threads/threads3.rs b/exercises/20_threads/threads3.rs index 91006bbc..7c44fa2a 100644 --- a/exercises/20_threads/threads3.rs +++ b/exercises/20_threads/threads3.rs @@ -3,7 +3,7 @@ // Execute `rustlings hint threads3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + use std::sync::mpsc; use std::sync::Arc; @@ -31,10 +31,12 @@ fn send_tx(q: Queue, tx: mpsc::Sender) -> () { let qc1 = Arc::clone(&qc); let qc2 = Arc::clone(&qc); + //这里克隆一下,避免使用所有权转移 + let tx_cloned = tx.clone(); thread::spawn(move || { for val in &qc1.first_half { println!("sending {:?}", val); - tx.send(*val).unwrap(); + tx_cloned.send(*val).unwrap(); thread::sleep(Duration::from_secs(1)); } }); diff --git a/exercises/21_macros/macros1.rs b/exercises/21_macros/macros1.rs index 678de6ee..87319302 100644 --- a/exercises/21_macros/macros1.rs +++ b/exercises/21_macros/macros1.rs @@ -3,7 +3,6 @@ // Execute `rustlings hint macros1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE macro_rules! my_macro { () => { @@ -12,5 +11,5 @@ macro_rules! my_macro { } fn main() { - my_macro(); + my_macro!(); } diff --git a/exercises/21_macros/macros2.rs b/exercises/21_macros/macros2.rs index 788fc16a..9d3417cd 100644 --- a/exercises/21_macros/macros2.rs +++ b/exercises/21_macros/macros2.rs @@ -3,12 +3,14 @@ // Execute `rustlings hint macros2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + fn main() { my_macro!(); } + +#[macro_export] macro_rules! my_macro { () => { println!("Check out my macro!"); diff --git a/exercises/21_macros/macros3.rs b/exercises/21_macros/macros3.rs index b795c149..def5e454 100644 --- a/exercises/21_macros/macros3.rs +++ b/exercises/21_macros/macros3.rs @@ -5,9 +5,10 @@ // Execute `rustlings hint macros3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + mod macros { + #[macro_export] //导出定义的宏 macro_rules! my_macro { () => { println!("Check out my macro!"); diff --git a/exercises/21_macros/macros4.rs b/exercises/21_macros/macros4.rs index 71b45a09..59b9ee45 100644 --- a/exercises/21_macros/macros4.rs +++ b/exercises/21_macros/macros4.rs @@ -3,13 +3,13 @@ // Execute `rustlings hint macros4` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + #[rustfmt::skip] macro_rules! my_macro { () => { println!("Check out my macro!"); - } + }; ($val:expr) => { println!("Look at this other macro: {}", $val); } diff --git a/exercises/22_clippy/Cargo.lock b/exercises/22_clippy/Cargo.lock new file mode 100644 index 00000000..d0fc406e --- /dev/null +++ b/exercises/22_clippy/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "clippy3" +version = "0.0.1" diff --git a/exercises/22_clippy/Cargo.toml b/exercises/22_clippy/Cargo.toml new file mode 100644 index 00000000..44dc91f8 --- /dev/null +++ b/exercises/22_clippy/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "clippy3" +version = "0.0.1" +edition = "2021" +[[bin]] +name = "clippy3" +path = "clippy3.rs" \ No newline at end of file diff --git a/exercises/22_clippy/clippy1.rs b/exercises/22_clippy/clippy1.rs index e0c6ce7c4..86b006e0 100644 --- a/exercises/22_clippy/clippy1.rs +++ b/exercises/22_clippy/clippy1.rs @@ -9,12 +9,13 @@ // Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + use std::f32; + fn main() { - let pi = 3.14f32; + let pi = std::f32::consts::PI; let radius = 5.00f32; let area = pi * f32::powi(radius, 2); diff --git a/exercises/22_clippy/clippy2.rs b/exercises/22_clippy/clippy2.rs index 9b87a0b7..1042f7b5 100644 --- a/exercises/22_clippy/clippy2.rs +++ b/exercises/22_clippy/clippy2.rs @@ -3,12 +3,10 @@ // Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - fn main() { let mut res = 42; let option = Some(12); - for x in option { + while let Some(x) = option{ res += x; } println!("{}", res); diff --git a/exercises/22_clippy/clippy3.rs b/exercises/22_clippy/clippy3.rs index 5a95f5b8..7c0c0750 100644 --- a/exercises/22_clippy/clippy3.rs +++ b/exercises/22_clippy/clippy3.rs @@ -3,28 +3,31 @@ // Here's a couple more easy Clippy fixes, so you can see its utility. // No hints. -// I AM NOT DONE #[allow(unused_variables, unused_assignments)] fn main() { let my_option: Option<()> = None; - if my_option.is_none() { - my_option.unwrap(); - } + // if my_option.is_none() { + // my_option.unwrap(); //这里怎么解决 + // } let my_arr = &[ - -1, -2, -3 + -1, -2, -3, -4, -5, -6 ]; println!("My array! Here it is: {:?}", my_arr); - let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5); + // let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5); + let mut my_empty_vec = vec![1, 2, 3, 4, 5]; + my_empty_vec.clear(); + println!("This Vec is empty, see? {:?}", my_empty_vec); let mut value_a = 45; let mut value_b = 66; // Let's swap these two! - value_a = value_b; - value_b = value_a; + // value_a = value_b; + // value_b = value_a; + std::mem::swap(&mut value_a, &mut value_b); println!("value a: {}; value b: {}", value_a, value_b); } diff --git a/exercises/23_conversions/as_ref_mut.rs b/exercises/23_conversions/as_ref_mut.rs index 2ba9e3f0..8499e226 100644 --- a/exercises/23_conversions/as_ref_mut.rs +++ b/exercises/23_conversions/as_ref_mut.rs @@ -7,25 +7,33 @@ // Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE // Obtain the number of bytes (not characters) in the given argument. // TODO: Add the AsRef trait appropriately as a trait bound. -fn byte_counter(arg: T) -> usize { +fn byte_counter(arg: T) -> usize +where T:AsRef +{ arg.as_ref().as_bytes().len() } // Obtain the number of characters (not bytes) in the given argument. // TODO: Add the AsRef trait appropriately as a trait bound. -fn char_counter(arg: T) -> usize { +fn char_counter(arg: T) -> usize +where T:AsRef +{ arg.as_ref().chars().count() } +//TODO 这里后续看看怎么有其他的方式进行修改 // Squares a number using as_mut(). // TODO: Add the appropriate trait bound. -fn num_sq(arg: &mut T) { +fn num_sq(arg: &mut T) +where T:AsMut +{ // TODO: Implement the function body. - ??? + let slice = arg.as_mut(); + *slice = *slice * *slice + } #[cfg(test)] diff --git a/exercises/23_conversions/from_into.rs b/exercises/23_conversions/from_into.rs index 60911f3e..4175fa27 100644 --- a/exercises/23_conversions/from_into.rs +++ b/exercises/23_conversions/from_into.rs @@ -40,10 +40,36 @@ impl Default for Person { // If while parsing the age, something goes wrong, then return the default of // Person Otherwise, then return an instantiated Person object with the results -// I AM NOT DONE - impl From<&str> for Person { fn from(s: &str) -> Person { + if s.len() == 0 { + return Person::default(); + } + let s_vec = s.split(",").collect::>(); + if s_vec.len() <2{ + return Person::default(); + } + let mut persion = Person::default(); + let name = s_vec[0].to_string(); + if name.len() == 0 { + return Person::default(); + } + let age = s_vec[1]; + + if age.len() == 0 { + return Person::default(); + } + + let age = if let Ok(age) = age.parse::() { + age + } else { + return Person::default(); + }; + + Person { + name: name, + age: age, + } } } diff --git a/exercises/23_conversions/from_str.rs b/exercises/23_conversions/from_str.rs index 34472c32..bc806837 100644 --- a/exercises/23_conversions/from_str.rs +++ b/exercises/23_conversions/from_str.rs @@ -31,7 +31,6 @@ enum ParsePersonError { ParseInt(ParseIntError), } -// I AM NOT DONE // Steps: // 1. If the length of the provided string is 0, an error should be returned @@ -49,12 +48,64 @@ enum ParsePersonError { // you want to return a string error message, you can do so via just using // return `Err("my error message".into())`. +//没有使用索引获取的 +// impl FromStr for Person { +// type Err = ParsePersonError; +// fn from_str(s: &str) -> Result { +// if s.is_empty(){ +// return Err(ParsePersonError::Empty); +// } +// let mut parts = s.split(","); +// if parts.clone().count() !=2{ +// return Err(ParsePersonError::BadLen); +// } +// let name= parts.next().unwrap_or("").to_string(); +// let age = parts.next().unwrap_or("").to_string(); +// if name.is_empty() { +// return Err(ParsePersonError::NoName); +// } +// let age = match age.parse::(){ +// Ok(age) =>age, +// Err(e) =>return Err(ParsePersonError::ParseInt(e)) +// }; +// Ok(Person{ +// name, +// age +// }) +// } +// } + + impl FromStr for Person { type Err = ParsePersonError; + fn from_str(s: &str) -> Result { + if s.is_empty() { + return Err(ParsePersonError::Empty); + } + + let parts: Vec<&str> = s.split(',').collect(); + + if parts.len() != 2 { + return Err(ParsePersonError::BadLen); + } + + let name = parts[0].to_string(); + if name.is_empty() { + return Err(ParsePersonError::NoName); + } + + let age = match parts[1].parse::() { + Ok(age) => age, + Err(e) => return Err(ParsePersonError::ParseInt(e)), + }; + + Ok(Person { name, age }) } } + + fn main() { let p = "Mark,20".parse::().unwrap(); println!("{:?}", p); diff --git a/exercises/23_conversions/try_from_into.rs b/exercises/23_conversions/try_from_into.rs index 32d6ef39..68655a6c 100644 --- a/exercises/23_conversions/try_from_into.rs +++ b/exercises/23_conversions/try_from_into.rs @@ -27,7 +27,7 @@ enum IntoColorError { IntConversion, } -// I AM NOT DONE + // Your task is to complete this implementation and return an Ok result of inner // type Color. You need to create an implementation for a tuple of three @@ -40,7 +40,26 @@ enum IntoColorError { // Tuple implementation impl TryFrom<(i16, i16, i16)> for Color { type Error = IntoColorError; - fn try_from(tuple: (i16, i16, i16)) -> Result { + fn try_from(tuple: (i16, i16, i16)) -> Result + { + let (red,green,blue) = tuple; + if red <0 ||red>255{ + return Err(IntoColorError::IntConversion) + } + if green <0 ||green>255{ + return Err(IntoColorError::IntConversion) + } + + if blue <0 ||blue>255{ + return Err(IntoColorError::IntConversion) + } + + + Ok(Color{ + red:red as u8, + green: green as u8, + blue:blue as u8, + }) } } @@ -48,6 +67,19 @@ impl TryFrom<(i16, i16, i16)> for Color { impl TryFrom<[i16; 3]> for Color { type Error = IntoColorError; fn try_from(arr: [i16; 3]) -> Result { + + for &value in &arr { + if value < 0 || value > 255 { + return Err(IntoColorError::IntConversion); + } + } + let arr = arr.iter().map(|f|*f as u8).collect::>(); + + Ok(Color{ + red:arr[0], + green:arr[1], + blue:arr[2], + }) } } @@ -55,6 +87,20 @@ impl TryFrom<[i16; 3]> for Color { impl TryFrom<&[i16]> for Color { type Error = IntoColorError; fn try_from(slice: &[i16]) -> Result { + if slice.len() ==3{ + for &value in slice { + if value < 0 || value > 255 { + return Err(IntoColorError::IntConversion); + } + } + Ok(Color{ + red:slice[0] as u8, + green:slice[1] as u8, + blue:slice[2] as u8, + }) + }else{ + return Err(IntoColorError::BadLen); + } } } diff --git a/exercises/23_conversions/using_as.rs b/exercises/23_conversions/using_as.rs index 414cef3a..54c90730 100644 --- a/exercises/23_conversions/using_as.rs +++ b/exercises/23_conversions/using_as.rs @@ -10,11 +10,10 @@ // Execute `rustlings hint using_as` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn average(values: &[f64]) -> f64 { let total = values.iter().sum::(); - total / values.len() + total / values.len() as f64 } fn main() { diff --git a/exercises/quiz3.rs b/exercises/quiz3.rs index 3b01d313..38bd6a40 100644 --- a/exercises/quiz3.rs +++ b/exercises/quiz3.rs @@ -16,15 +16,14 @@ // // 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 { +impl ReportCard { pub fn print(&self) -> String { format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade) @@ -52,7 +51,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, };