From 50f23f7e482eb958f2375ab206feb14a0edc49d7 Mon Sep 17 00:00:00 2001 From: AramJonghu Date: Sun, 13 Sep 2026 16:04:15 +0200 Subject: [PATCH] feat: progress iterators --- exercises/18_iterators/iterators2.rs | 6 +++++- exercises/18_iterators/iterators3.rs | 16 +++++++++++++--- exercises/18_iterators/iterators4.rs | 2 ++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/exercises/18_iterators/iterators2.rs b/exercises/18_iterators/iterators2.rs index ff140db..ed1c785 100644 --- a/exercises/18_iterators/iterators2.rs +++ b/exercises/18_iterators/iterators2.rs @@ -1,13 +1,15 @@ // In this exercise, you'll learn some of the unique advantages that iterators // can offer. +// I failed. + // TODO: Complete the `capitalize_first` function. // "hello" -> "Hello" fn capitalize_first(input: &str) -> String { let mut chars = input.chars(); match chars.next() { None => String::new(), - Some(first) => char::to_uppercase(first).to_string(), + Some(first) => first.to_uppercase().to_string() + chars.as_str(), } } @@ -16,6 +18,7 @@ fn capitalize_first(input: &str) -> String { // ["hello", "world"] -> ["Hello", "World"] fn capitalize_words_vector(words: &[&str]) -> Vec { // ??? + words.iter().map(|word| capitalize_first(word)).collect() } // TODO: Apply the `capitalize_first` function again to a slice of string @@ -23,6 +26,7 @@ fn capitalize_words_vector(words: &[&str]) -> Vec { // ["hello", " ", "world"] -> "Hello World" fn capitalize_words_string(words: &[&str]) -> String { // ??? + words.iter().map(|word| capitalize_first(word)).collect() } fn main() { diff --git a/exercises/18_iterators/iterators3.rs b/exercises/18_iterators/iterators3.rs index dce0905..19bde3a 100644 --- a/exercises/18_iterators/iterators3.rs +++ b/exercises/18_iterators/iterators3.rs @@ -11,21 +11,31 @@ enum DivisionError { // TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`. // Otherwise, return a suitable error. fn divide(a: i64, b: i64) -> Result { - todo!(); + if b == 0 { + Err(DivisionError::DivideByZero) + } else if b == -1 && a == i64::MIN { + Err(DivisionError::IntegerOverflow) + } else if a % b == 0 { + Ok(a / b) + } else { + Err(DivisionError::NotDivisible) + } } // TODO: Add the correct return type and complete the function body. // Desired output: `Ok([1, 11, 1426, 3])` -fn result_with_list() { +fn result_with_list() -> Result, DivisionError> { let numbers = [27, 297, 38502, 81]; let division_results = numbers.into_iter().map(|n| divide(n, 27)); + division_results.collect() } // TODO: Add the correct return type and complete the function body. // Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]` -fn list_of_results() { +fn list_of_results() -> Vec> { let numbers = [27, 297, 38502, 81]; let division_results = numbers.into_iter().map(|n| divide(n, 27)); + division_results.collect() } fn main() { diff --git a/exercises/18_iterators/iterators4.rs b/exercises/18_iterators/iterators4.rs index c296f0e..6d090f7 100644 --- a/exercises/18_iterators/iterators4.rs +++ b/exercises/18_iterators/iterators4.rs @@ -10,6 +10,8 @@ fn factorial(num: u64) -> u64 { // - additional variables // For an extra challenge, don't use: // - recursion + + (1..=num).product() } fn main() {