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
exercises/standard_library_types

View file

@ -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;

View file

@ -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");
}
}

View file

@ -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 {