From 52a29aa84be2a89894f2ad1f5ebdcf153c49f399 Mon Sep 17 00:00:00 2001 From: magnusrodseth <59113973+magnusrodseth@users.noreply.github.com> Date: Wed, 17 Aug 2022 12:50:34 +0200 Subject: [PATCH 1/2] test: Convert main function to working tests --- exercises/options/options2.rs | 41 ++++++++++++++++++++++------------- info.toml | 2 +- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs index 75b66a3..eca03f0 100644 --- a/exercises/options/options2.rs +++ b/exercises/options/options2.rs @@ -3,23 +3,34 @@ // 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 { - println!("The word is: {}", word); - } else { - println!("The optional word doesn't contain anything"); +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn simple_option() { + let target = "rustlings"; + let optional_target = Some(target); + + // TODO: Make this an if let statement whose value is "Some" type + if let Some(word) = optional_target { + assert_eq!(word, target); + } } - let mut optional_integers_vec: Vec> = Vec::new(); - for x in 1..10 { - optional_integers_vec.push(Some(x)); - } + #[test] + fn layered_option() { + let mut range = 10; + let mut optional_integers: Vec> = Vec::new(); + for i in 0..(range + 1) { + optional_integers.push(Some(i)); + } - // 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_vec.pop() { - println!("current value: {}", integer); + // 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 + while let Some(Some(integer)) = optional_integers.pop() { + assert_eq!(integer, range); + range -= 1; + } } } diff --git a/info.toml b/info.toml index 9aa11e8..8bce721 100644 --- a/info.toml +++ b/info.toml @@ -545,7 +545,7 @@ is the easiest, but how do you do it safely so that it doesn't panic in your fac [[exercises]] name = "options2" path = "exercises/options/options2.rs" -mode = "compile" +mode = "test" hint = """ check out: https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html From 99ea2cbba7dabb011fc76e4480e27f0bbc6149ad Mon Sep 17 00:00:00 2001 From: magnusrodseth <59113973+magnusrodseth@users.noreply.github.com> Date: Wed, 17 Aug 2022 12:54:23 +0200 Subject: [PATCH 2/2] chore: make options2 not compile deliberately --- exercises/options/options2.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs index eca03f0..b112047 100644 --- a/exercises/options/options2.rs +++ b/exercises/options/options2.rs @@ -13,7 +13,7 @@ mod tests { let optional_target = Some(target); // TODO: Make this an if let statement whose value is "Some" type - if let Some(word) = optional_target { + word = optional_target { assert_eq!(word, target); } } @@ -28,7 +28,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 - while let Some(Some(integer)) = optional_integers.pop() { + integer = optional_integers.pop() { assert_eq!(integer, range); range -= 1; }