half iters done

This commit is contained in:
David E. Perez Negron R. 2024-11-08 21:19:57 -06:00
parent 3db6c0baf4
commit 06c8a197fa
3 changed files with 23 additions and 11 deletions

View File

@ -9,18 +9,17 @@
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a // Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
#[test] #[test]
fn main() { fn main() {
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"]; 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(), 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(), 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(), Some(&"raspberry"));
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4 assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
} }

View File

@ -6,7 +6,6 @@
// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a // Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
// Step 1. // Step 1.
// Complete the `capitalize_first` function. // Complete the `capitalize_first` function.
@ -15,8 +14,11 @@ pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars(); let mut c = input.chars();
match c.next() { match c.next() {
None => String::new(), None => String::new(),
Some(first) => ???, Some(first) => {
first.to_ascii_uppercase().to_string() + c.as_str()
},
} }
} }
// Step 2. // Step 2.
@ -24,7 +26,7 @@ pub fn capitalize_first(input: &str) -> String {
// Return a vector of strings. // Return a vector of strings.
// ["hello", "world"] -> ["Hello", "World"] // ["hello", "world"] -> ["Hello", "World"]
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> { pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
vec![] words.iter().map(|x| capitalize_first(x)).collect()
} }
// Step 3. // Step 3.
@ -32,7 +34,8 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
// Return a single string. // Return a single string.
// ["hello", " ", "world"] -> "Hello World" // ["hello", " ", "world"] -> "Hello World"
pub fn capitalize_words_string(words: &[&str]) -> String { 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)] #[cfg(test)]

View File

@ -26,7 +26,16 @@ pub struct NotDivisibleError {
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`. // Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
// Otherwise, return a suitable error. // Otherwise, return a suitable error.
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> { pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
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 // Complete the function and return a value of the correct type so the test
@ -35,6 +44,7 @@ pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
fn result_with_list() -> () { fn result_with_list() -> () {
let numbers = vec![27, 297, 38502, 81]; 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 // Complete the function and return a value of the correct type so the test