things going to be harder and harder

This commit is contained in:
tab 2020-05-14 23:34:23 +08:00
parent 398aacb513
commit eea0a2957e
3 changed files with 18 additions and 9 deletions

View File

@ -4,17 +4,16 @@
// somewhere. Try not to create any copies of the `numbers` Vec! // somewhere. Try not to create any copies of the `numbers` Vec!
// Execute `rustlings hint arc1` for hints :) // Execute `rustlings hint arc1` for hints :)
// I AM NOT DONE
use std::sync::Arc; use std::sync::Arc;
use std::thread; use std::thread;
fn main() { fn main() {
let numbers: Vec<_> = (0..100u32).collect(); let numbers: Vec<_> = (0..100u32).collect();
let shared_numbers = // TODO let shared_numbers = Arc::new(numbers);
let mut joinhandles = Vec::new(); let mut joinhandles = Vec::new();
for offset in 0..8 { for offset in 0..8 {
let child_numbers = shared_numbers.clone();
joinhandles.push(thread::spawn(move || { joinhandles.push(thread::spawn(move || {
let mut i = offset; let mut i = offset;
let mut sum = 0; let mut sum = 0;

View File

@ -7,13 +7,11 @@
// Try to ensure it returns a single string. // Try to ensure it returns a single string.
// As always, there are hints if you execute `rustlings hint iterators2`! // As always, there are hints if you execute `rustlings hint iterators2`!
// I AM NOT DONE
pub fn capitalize_first(input: &str) -> String { pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars(); let mut c = input.chars();
match c.next() { match c.next() {
None => String::new(), 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] #[test]
fn test_iterate_string_vec() { fn test_iterate_string_vec() {
let words = vec!["hello", "world"]; 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"]); assert_eq!(capitalized_words, ["Hello", "World"]);
} }
#[test] #[test]
fn test_iterate_into_string() { fn test_iterate_into_string() {
let words = vec!["hello", " ", "world"]; 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"); assert_eq!(capitalized_words, "Hello World");
} }
} }

View File

@ -24,7 +24,19 @@ pub struct NotDivisibleError {
// This function should calculate `a` divided by `b` if `a` is // This function should calculate `a` divided by `b` if `a` is
// evenly divisible by b. // evenly divisible by b.
// Otherwise, it should return a suitable error. // 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)] #[cfg(test)]
mod tests { mod tests {