Solve exercise clippy, arc1 and iterators2

This commit is contained in:
Enrico Bozzolini 2020-05-29 22:02:28 +02:00
parent d3a2dd6b64
commit e0dd3844fc
4 changed files with 13 additions and 14 deletions

View File

@ -6,12 +6,10 @@
// check clippy's suggestions from the output to solve the exercise.
// Execute `rustlings hint clippy1` for hints :)
// I AM NOT DONE
fn main() {
let x = 1.2331f64;
let y = 1.2332f64;
if y != x {
if (x - y).abs() < 0.00001 {
println!("Success!");
}
}

View File

@ -1,12 +1,10 @@
// clippy2.rs
// Make me compile! Execute `rustlings hint clippy2` for hints :)
// I AM NOT DONE
fn main() {
let mut res = 42;
let option = Some(12);
for x in option {
if let Some(x) = option {
res += x;
}
println!("{}", res);

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,16 +7,19 @@
// 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(),
// TODO: Check why we would use collect() here?
Some(first) => first.to_uppercase().to_string() + c.as_str(),
}
}
pub fn capitalize_words(input: Vec<&str>) -> Vec<String> {
input.iter().map(|word| capitalize_first(word)).collect::<Vec<String>>()
}
#[cfg(test)]
mod tests {
use super::*;
@ -26,6 +29,7 @@ mod tests {
#[test]
fn test_success() {
assert_eq!(capitalize_first("hello"), "Hello");
assert_eq!(capitalize_first("é"), "É");
}
#[test]
@ -37,14 +41,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> = capitalize_words(words);
assert_eq!(capitalized_words, ["Hello", "World"]);
}
#[test]
fn test_iterate_into_string() {
let words = vec!["hello", " ", "world"];
let capitalized_words = // TODO
let capitalized_words = capitalize_words(words).join("");
assert_eq!(capitalized_words, "Hello World");
}
}