From 06c8a197fa65165d94fd5611013ba5cf75f2d505 Mon Sep 17 00:00:00 2001 From: "David E. Perez Negron R." Date: Fri, 8 Nov 2024 21:19:57 -0600 Subject: [PATCH] half iters done --- exercises/iterators/iterators1.rs | 9 ++++----- exercises/iterators/iterators2.rs | 11 +++++++---- exercises/iterators/iterators3.rs | 14 ++++++++++++-- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/exercises/iterators/iterators1.rs b/exercises/iterators/iterators1.rs index 31076bb..6d84c0a 100644 --- a/exercises/iterators/iterators1.rs +++ b/exercises/iterators/iterators1.rs @@ -9,18 +9,17 @@ // Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE #[test] fn main() { let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"]; - let mut my_iterable_fav_fruits = ???; // TODO: Step 1 + let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1 assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2 + assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2 assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3 + assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3 assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4 + assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4 } diff --git a/exercises/iterators/iterators2.rs b/exercises/iterators/iterators2.rs index dda82a0..e5b3eae 100644 --- a/exercises/iterators/iterators2.rs +++ b/exercises/iterators/iterators2.rs @@ -6,7 +6,6 @@ // Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE // Step 1. // Complete the `capitalize_first` function. @@ -15,8 +14,11 @@ pub fn capitalize_first(input: &str) -> String { let mut c = input.chars(); match c.next() { None => String::new(), - Some(first) => ???, + Some(first) => { + first.to_ascii_uppercase().to_string() + c.as_str() + }, } + } // Step 2. @@ -24,7 +26,7 @@ pub fn capitalize_first(input: &str) -> String { // Return a vector of strings. // ["hello", "world"] -> ["Hello", "World"] pub fn capitalize_words_vector(words: &[&str]) -> Vec { - vec![] + words.iter().map(|x| capitalize_first(x)).collect() } // Step 3. @@ -32,7 +34,8 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec { // Return a single string. // ["hello", " ", "world"] -> "Hello World" pub fn capitalize_words_string(words: &[&str]) -> String { - String::new() + let one_string: String = words.iter().map(|x| capitalize_first(x)).collect(); + one_string } #[cfg(test)] diff --git a/exercises/iterators/iterators3.rs b/exercises/iterators/iterators3.rs index 29fa23a..b1b96a6 100644 --- a/exercises/iterators/iterators3.rs +++ b/exercises/iterators/iterators3.rs @@ -26,7 +26,16 @@ pub struct NotDivisibleError { // Calculate `a` divided by `b` if `a` is evenly divisible by `b`. // Otherwise, return a suitable error. pub fn divide(a: i32, b: i32) -> Result { - todo!(); + if b == 0 { + Err(DivisionError::DivideByZero) + } else if a % b == 0 { + Ok(a / b) + } else { + Err(DivisionError::NotDivisible(NotDivisibleError { + dividend: a, + divisor: b + })) + } } // Complete the function and return a value of the correct type so the test @@ -34,7 +43,8 @@ pub fn divide(a: i32, b: i32) -> Result { // Desired output: Ok([1, 11, 1426, 3]) fn result_with_list() -> () { let numbers = vec![27, 297, 38502, 81]; - let division_results = numbers.into_iter().map(|n| divide(n, 27)); + let division_results = numbers.into_iter().map(|n| divide(n, 27)); + division_results.filter(|x| Ok(x)).collect() } // Complete the function and return a value of the correct type so the test