From eea0a2957e266eeaf9129e9f1ea3275d6af686ea Mon Sep 17 00:00:00 2001 From: tab <dearhange@126.com> Date: Thu, 14 May 2020 23:34:23 +0800 Subject: [PATCH] things going to be harder and harder --- exercises/standard_library_types/arc1.rs | 5 ++--- exercises/standard_library_types/iterators2.rs | 8 +++----- exercises/standard_library_types/iterators3.rs | 14 +++++++++++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/exercises/standard_library_types/arc1.rs b/exercises/standard_library_types/arc1.rs index 9784e84..f5e99bf 100644 --- a/exercises/standard_library_types/arc1.rs +++ b/exercises/standard_library_types/arc1.rs @@ -4,17 +4,16 @@ // somewhere. Try not to create any copies of the `numbers` Vec! // Execute `rustlings hint arc1` for hints :) -// I AM NOT DONE - use std::sync::Arc; use std::thread; fn main() { let numbers: Vec<_> = (0..100u32).collect(); - let shared_numbers = // TODO + let shared_numbers = Arc::new(numbers); let mut joinhandles = Vec::new(); for offset in 0..8 { + let child_numbers = shared_numbers.clone(); joinhandles.push(thread::spawn(move || { let mut i = offset; let mut sum = 0; diff --git a/exercises/standard_library_types/iterators2.rs b/exercises/standard_library_types/iterators2.rs index 837725f..8ebf15f 100644 --- a/exercises/standard_library_types/iterators2.rs +++ b/exercises/standard_library_types/iterators2.rs @@ -7,13 +7,11 @@ // Try to ensure it returns a single string. // As always, there are hints if you execute `rustlings hint iterators2`! -// I AM NOT DONE - pub fn capitalize_first(input: &str) -> String { let mut c = input.chars(); match c.next() { None => String::new(), - Some(first) => first.collect::<String>() + c.as_str(), + Some(first) => first.to_uppercase().collect::<String>() + c.as_str(), } } @@ -37,14 +35,14 @@ mod tests { #[test] fn test_iterate_string_vec() { let words = vec!["hello", "world"]; - let capitalized_words: Vec<String> = // TODO + let capitalized_words: Vec<String> = words.iter().map(|x| capitalize_first(x)).collect(); assert_eq!(capitalized_words, ["Hello", "World"]); } #[test] fn test_iterate_into_string() { let words = vec!["hello", " ", "world"]; - let capitalized_words = // TODO + let capitalized_words: String = words.iter().map(|x| capitalize_first(x)).collect(); assert_eq!(capitalized_words, "Hello World"); } } diff --git a/exercises/standard_library_types/iterators3.rs b/exercises/standard_library_types/iterators3.rs index 353cea6..4bed64f 100644 --- a/exercises/standard_library_types/iterators3.rs +++ b/exercises/standard_library_types/iterators3.rs @@ -24,7 +24,19 @@ pub struct NotDivisibleError { // This function should calculate `a` divided by `b` if `a` is // evenly divisible by b. // Otherwise, it should return a suitable error. -pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {} +pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> { + match (a, b) { + (a, 0) => Err(DivisionError::DivideByZero), + (0, b) => Ok(0), + _ => match a % b { + 0 => Ok(a / b), + _ => Err(DivisionError::NotDivisible(NotDivisibleError { + dividend: a, + divisor: b, + })), + }, + } +} #[cfg(test)] mod tests {