From 10dabcd66c13354ec79570860318330bb027ee7a Mon Sep 17 00:00:00 2001 From: Tyler Cardinal <83625450+tjcardinal@users.noreply.github.com> Date: Thu, 13 May 2021 21:40:20 -0500 Subject: [PATCH] Complete option exercises (#15) --- exercises/option/option1.rs | 10 ++++------ exercises/option/option2.rs | 9 ++------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/exercises/option/option1.rs b/exercises/option/option1.rs index 602ff1a..7500319 100644 --- a/exercises/option/option1.rs +++ b/exercises/option/option1.rs @@ -1,23 +1,21 @@ // option1.rs // Make me compile! Execute `rustlings hint option1` for hints -// I AM NOT DONE - // you can modify anything EXCEPT for this function's sig fn print_number(maybe_number: Option<u16>) { println!("printing: {}", maybe_number.unwrap()); } fn main() { - print_number(13); - print_number(99); + print_number(Some(13)); + print_number(Some(99)); - let mut numbers: [Option<u16>; 5]; + let mut numbers: [Option<u16>; 5] = [None; 5]; for iter in 0..5 { let number_to_add: u16 = { ((iter * 1235) + 2) / (4 * 16) }; - numbers[iter as usize] = number_to_add; + numbers[iter as usize] = Some(number_to_add); } } diff --git a/exercises/option/option2.rs b/exercises/option/option2.rs index c6b83ec..0e26fdb 100644 --- a/exercises/option/option2.rs +++ b/exercises/option/option2.rs @@ -1,12 +1,9 @@ // option2.rs // Make me compile! Execute `rustlings hint option2` for hints -// I AM NOT DONE - fn main() { let optional_word = Some(String::from("rustlings")); - // TODO: Make this an if let statement whose value is "Some" type - word = optional_word { + if let Some(word) = optional_word { println!("The word is: {}", word); } else { println!("The optional word doesn't contain anything"); @@ -17,9 +14,7 @@ fn main() { optional_integers_vec.push(Some(x)); } - // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T> - // You can stack `Option<T>`'s into while let and if let - integer = optional_integers_vec.pop() { + while let Some(Some(integer)) = optional_integers_vec.pop() { println!("current value: {}", integer); } }