From 416365a0147f832459df644ff6d2536b81b9a004 Mon Sep 17 00:00:00 2001 From: Esteban Escobar Date: Sat, 21 Jan 2023 22:44:28 -0500 Subject: [PATCH] saving more progress --- REVISITS.md | 3 ++- exercises/options/options1.rs | 12 ++++++++---- exercises/options/options2.rs | 6 ++---- exercises/options/options3.rs | 4 +--- exercises/quiz2.rs | 13 ++++++++----- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/REVISITS.md b/REVISITS.md index 973cc0c7..3982c070 100644 --- a/REVISITS.md +++ b/REVISITS.md @@ -13,5 +13,6 @@ return results to new Vec vector 12) generics2. review https://doc.rust-lang.org/book/ch10-01-syntax.html 13) traits1 why is self accepted since it's being updated? 14) revisit all traits exercises - +15) options1 (understand Some and None) +16) option2 (important to understand if let, while let statements alternative to match ). ChatGPT explanation was great. diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs index 1f891b0e..f15f61b6 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -1,8 +1,6 @@ // options1.rs // Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - // This function returns how much icecream there is left in the fridge. // If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them // all, so there'll be no more left :( @@ -10,7 +8,13 @@ fn maybe_icecream(time_of_day: u16) -> Option { // We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a value of 0 // The Option output should gracefully handle cases where time_of_day > 23. // TODO: Complete the function body - remember to return an Option! - ??? + + match time_of_day { + n if n < 22 => Some(5), + n if n >= 22 && n < 24 => Some(0), + n => None + } + } #[cfg(test)] @@ -30,6 +34,6 @@ mod tests { fn raw_value() { // TODO: Fix this test. How do you get at the value contained in the Option? let icecreams = maybe_icecream(12); - assert_eq!(icecreams, 5); + assert_eq!(icecreams.unwrap(), 5); } } diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs index b1120471..1769e96f 100644 --- a/exercises/options/options2.rs +++ b/exercises/options/options2.rs @@ -1,8 +1,6 @@ // options2.rs // Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[cfg(test)] mod tests { use super::*; @@ -13,7 +11,7 @@ mod tests { let optional_target = Some(target); // TODO: Make this an if let statement whose value is "Some" type - word = optional_target { + if let Some(word) = optional_target { assert_eq!(word, target); } } @@ -28,7 +26,7 @@ mod tests { // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option // You can stack `Option`'s into while let and if let - integer = optional_integers.pop() { + while let Some(Some(integer)) = optional_integers.pop() { assert_eq!(integer, range); range -= 1; } diff --git a/exercises/options/options3.rs b/exercises/options/options3.rs index 3f995c52..c7f124a6 100644 --- a/exercises/options/options3.rs +++ b/exercises/options/options3.rs @@ -1,8 +1,6 @@ // options3.rs // Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - struct Point { x: i32, y: i32, @@ -11,7 +9,7 @@ struct Point { fn main() { let y: Option = Some(Point { x: 100, y: 200 }); - match y { + match &y { Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), _ => println!("no match"), } diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 715788b8..06b168d0 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -18,8 +18,6 @@ // - The output element is going to be a Vector of strings. // No hints this time! -// I AM NOT DONE - pub enum Command { Uppercase, Trim, @@ -30,11 +28,16 @@ mod my_module { use super::Command; // TODO: Complete the function signature! - pub fn transformer(input: ???) -> ??? { + pub fn transformer(input: Vec<(String, Command)>) -> Vec { // TODO: Complete the output declaration! - let mut output: ??? = vec![]; + let mut output: Vec = vec![]; for (string, command) in input.iter() { // TODO: Complete the function body. You can do it! + match command { + Command::Uppercase => output.push(string.to_uppercase()), + Command::Trim => output.push(string.trim().to_string()), + Command::Append(n) => output.push(format!("{}{}",string, "bar".repeat(*n))) + } } output } @@ -43,7 +46,7 @@ mod my_module { #[cfg(test)] mod tests { // TODO: What do we need to import to have `transformer` in scope? - use ???; + use my_module::transformer as transformer; use super::Command; #[test]