mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-05 10:19:18 +00:00
Solve exercise clippy, arc1 and iterators2
This commit is contained in:
parent
d3a2dd6b64
commit
e0dd3844fc
@ -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!");
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user