feat: progress iterators

This commit is contained in:
2026-09-13 18:23:25 +02:00
parent 88b981b50c
commit 50f23f7e48
3 changed files with 20 additions and 4 deletions
+5 -1
View File
@@ -1,13 +1,15 @@
// In this exercise, you'll learn some of the unique advantages that iterators // In this exercise, you'll learn some of the unique advantages that iterators
// can offer. // can offer.
// I failed.
// TODO: Complete the `capitalize_first` function. // TODO: Complete the `capitalize_first` function.
// "hello" -> "Hello" // "hello" -> "Hello"
fn capitalize_first(input: &str) -> String { fn capitalize_first(input: &str) -> String {
let mut chars = input.chars(); let mut chars = input.chars();
match chars.next() { match chars.next() {
None => String::new(), 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"] // ["hello", "world"] -> ["Hello", "World"]
fn capitalize_words_vector(words: &[&str]) -> Vec<String> { fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
// ??? // ???
words.iter().map(|word| capitalize_first(word)).collect()
} }
// TODO: Apply the `capitalize_first` function again to a slice of string // TODO: Apply the `capitalize_first` function again to a slice of string
@@ -23,6 +26,7 @@ fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
// ["hello", " ", "world"] -> "Hello World" // ["hello", " ", "world"] -> "Hello World"
fn capitalize_words_string(words: &[&str]) -> String { fn capitalize_words_string(words: &[&str]) -> String {
// ??? // ???
words.iter().map(|word| capitalize_first(word)).collect()
} }
fn main() { fn main() {
+13 -3
View File
@@ -11,21 +11,31 @@ enum DivisionError {
// TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`. // TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
// Otherwise, return a suitable error. // Otherwise, return a suitable error.
fn divide(a: i64, b: i64) -> Result<i64, DivisionError> { fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
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. // TODO: Add the correct return type and complete the function body.
// Desired output: `Ok([1, 11, 1426, 3])` // Desired output: `Ok([1, 11, 1426, 3])`
fn result_with_list() { fn result_with_list() -> Result<Vec<i64>, DivisionError> {
let numbers = [27, 297, 38502, 81]; let numbers = [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.collect()
} }
// TODO: Add the correct return type and complete the function body. // TODO: Add the correct return type and complete the function body.
// Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]` // Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]`
fn list_of_results() { fn list_of_results() -> Vec<Result<i64, DivisionError>> {
let numbers = [27, 297, 38502, 81]; let numbers = [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.collect()
} }
fn main() { fn main() {
+2
View File
@@ -10,6 +10,8 @@ fn factorial(num: u64) -> u64 {
// - additional variables // - additional variables
// For an extra challenge, don't use: // For an extra challenge, don't use:
// - recursion // - recursion
(1..=num).product()
} }
fn main() { fn main() {