Merge pull request #1150 from magnusrodseth/1109-options2-test

`options2`: Convert the main function into tests
This commit is contained in:
liv 2022-08-18 11:53:16 +02:00 committed by GitHub
commit 44d609816b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 16 deletions

View file

@ -3,23 +3,34 @@
// I AM NOT DONE // I AM NOT DONE
fn main() { #[cfg(test)]
let optional_word = Some(String::from("rustlings")); 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 // TODO: Make this an if let statement whose value is "Some" type
word = optional_word { word = optional_target {
println!("The word is: {}", word); assert_eq!(word, target);
} else { }
println!("The optional word doesn't contain anything");
} }
let mut optional_integers_vec: Vec<Option<i8>> = Vec::new(); #[test]
for x in 1..10 { fn layered_option() {
optional_integers_vec.push(Some(x)); let mut range = 10;
let mut optional_integers: Vec<Option<i8>> = 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<T> // 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 // You can stack `Option<T>`'s into while let and if let
integer = optional_integers_vec.pop() { integer = optional_integers.pop() {
println!("current value: {}", integer); assert_eq!(integer, range);
range -= 1;
}
} }
} }

View file

@ -545,7 +545,7 @@ is the easiest, but how do you do it safely so that it doesn't panic in your fac
[[exercises]] [[exercises]]
name = "options2" name = "options2"
path = "exercises/options/options2.rs" path = "exercises/options/options2.rs"
mode = "compile" mode = "test"
hint = """ hint = """
check out: check out:
https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html