Skip to content

Commit

Permalink
Merge pull request rust-lang#1150 from magnusrodseth/1109-options2-test
Browse files Browse the repository at this point in the history
`options2`: Convert the main function into tests
  • Loading branch information
shadows-withal authored Aug 18, 2022
2 parents d0c7b06 + 99ea2cb commit 44d6098
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
41 changes: 26 additions & 15 deletions exercises/options/options2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

let mut optional_integers_vec: Vec<Option<i8>> = Vec::new();
for x in 1..10 {
optional_integers_vec.push(Some(x));
// TODO: Make this an if let statement whose value is "Some" type
word = optional_target {
assert_eq!(word, target);
}
}

// 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() {
println!("current value: {}", integer);
#[test]
fn layered_option() {
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>
// You can stack `Option<T>`'s into while let and if let
integer = optional_integers.pop() {
assert_eq!(integer, range);
range -= 1;
}
}
}
2 changes: 1 addition & 1 deletion info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 44d6098

Please sign in to comment.