From 9381dc4ed3f5b6b8112d477a488e1aa782fd8d34 Mon Sep 17 00:00:00 2001 From: tab <dearhange@126.com> Date: Mon, 18 May 2020 23:19:43 +0800 Subject: [PATCH] complit!! --- exercises/conversions/as_ref_mut.rs | 5 ++--- exercises/conversions/from_into.rs | 11 ++++++++++- exercises/conversions/from_str.rs | 13 +++++++++++-- exercises/conversions/try_from_into.rs | 13 +++++++++++-- exercises/conversions/using_as.rs | 5 ++--- exercises/threads/threads1.rs | 9 ++++----- 6 files changed, 40 insertions(+), 16 deletions(-) diff --git a/exercises/conversions/as_ref_mut.rs b/exercises/conversions/as_ref_mut.rs index aa1e623..a9ccd91 100644 --- a/exercises/conversions/as_ref_mut.rs +++ b/exercises/conversions/as_ref_mut.rs @@ -2,16 +2,15 @@ // Read more about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html // and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively. -// I AM NOT DONE // Obtain the number of bytes (not characters) in the given argument // Add the AsRef trait appropriately as a trait bound -fn byte_counter<T>(arg: T) -> usize { +fn byte_counter<T: AsRef<str>>(arg: T) -> usize { arg.as_ref().as_bytes().len() } // Obtain the number of characters (not bytes) in the given argument // Add the AsRef trait appropriately as a trait bound -fn char_counter<T>(arg: T) -> usize { +fn char_counter<T: AsRef<str>>(arg: T) -> usize { arg.as_ref().chars().count() } diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs index 8fb9eb0..b670a1b 100644 --- a/exercises/conversions/from_into.rs +++ b/exercises/conversions/from_into.rs @@ -18,7 +18,6 @@ impl Default for Person { } } -// I AM NOT DONE // Your task is to complete this implementation // in order for the line `let p = Person::from("Mark,20")` to compile // Please note that you'll need to parse the age component into a `usize` @@ -35,6 +34,16 @@ impl Default for Person { // Otherwise, then return an instantiated Person object with the results impl From<&str> for Person { fn from(s: &str) -> Person { + let v: Vec<&str> = s.split(',').collect(); + if v.len() < 2 { + return Person::default(); + } + let age = v[1].parse(); + if v[0].is_empty() || age.is_err() { + Person::default() + } else { + Person{name: v[0].to_string(), age: age.unwrap()} + } } } diff --git a/exercises/conversions/from_str.rs b/exercises/conversions/from_str.rs index 14e9e09..e95fe04 100644 --- a/exercises/conversions/from_str.rs +++ b/exercises/conversions/from_str.rs @@ -10,7 +10,6 @@ struct Person { age: usize, } -// I AM NOT DONE // Steps: // 1. If the length of the provided string is 0, then return an error // 2. Split the given string on the commas present in it @@ -22,6 +21,16 @@ struct Person { impl FromStr for Person { type Err = String; fn from_str(s: &str) -> Result<Person, Self::Err> { + let v: Vec<&str> = s.split(",").collect(); + if v.len() < 2 { + return Err("need to args".to_string()); + } + let age = v[1].parse(); + if v[0].is_empty() || age.is_err() { + Err("wrong".to_string()) + } else { + Ok(Person{name: v[0].to_string(), age: age.unwrap()}) + } } } @@ -82,4 +91,4 @@ mod tests { ",one".parse::<Person>().unwrap(); } -} \ No newline at end of file +} diff --git a/exercises/conversions/try_from_into.rs b/exercises/conversions/try_from_into.rs index 9968075..c33fab8 100644 --- a/exercises/conversions/try_from_into.rs +++ b/exercises/conversions/try_from_into.rs @@ -10,7 +10,6 @@ struct Person { age: usize, } -// I AM NOT DONE // Your task is to complete this implementation // in order for the line `let p = Person::try_from("Mark,20")` to compile // and return an Ok result of inner type Person. @@ -29,6 +28,16 @@ struct Person { impl TryFrom<&str> for Person { type Error = String; fn try_from(s: &str) -> Result<Self, Self::Error> { + let v: Vec<&str> = s.split(",").collect(); + if v.len() < 2 { + return Err("need to args".to_string()); + } + let age = v[1].parse(); + if v[0].is_empty() || age.is_err() { + Err("wrong".to_string()) + } else { + Ok(Person{name: v[0].to_string(), age: age.unwrap()}) + } } } @@ -99,4 +108,4 @@ mod tests { fn test_missing_name_and_invalid_age() { let _: Person = ",one".try_into().unwrap(); } -} \ No newline at end of file +} diff --git a/exercises/conversions/using_as.rs b/exercises/conversions/using_as.rs index 54f9651..dbefab3 100644 --- a/exercises/conversions/using_as.rs +++ b/exercises/conversions/using_as.rs @@ -2,16 +2,15 @@ // Please note that the `as` operator is not only used when type casting. // It also helps with renaming imports. -// I AM NOT DONE // The goal is to make sure that the division does not fail to compile fn average(values: &[f64]) -> f64 { let total = values .iter() .fold(0.0, |a, b| a + b); - total / values.len() + total / values.len() as f64 } fn main() { let values = [3.5, 0.3, 13.0, 11.7]; println!("{}", average(&values)); -} \ No newline at end of file +} diff --git a/exercises/threads/threads1.rs b/exercises/threads/threads1.rs index 1785e8c..b324261 100644 --- a/exercises/threads/threads1.rs +++ b/exercises/threads/threads1.rs @@ -5,9 +5,8 @@ // of "waiting..." and the program ends without timing out when running, // you've got it :) -// I AM NOT DONE - use std::sync::Arc; +use std::sync::Mutex; use std::thread; use std::time::Duration; @@ -16,15 +15,15 @@ struct JobStatus { } fn main() { - let status = Arc::new(JobStatus { jobs_completed: 0 }); + let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 })); let status_shared = status.clone(); thread::spawn(move || { for _ in 0..10 { thread::sleep(Duration::from_millis(250)); - status_shared.jobs_completed += 1; + status_shared.lock().unwrap().jobs_completed += 1; } }); - while status.jobs_completed < 10 { + while status.lock().unwrap().jobs_completed < 10 { println!("waiting... "); thread::sleep(Duration::from_millis(500)); }