finished options

This commit is contained in:
Justin Kelz 2022-05-01 10:54:16 -07:00
parent ccd2721335
commit a58652ac54
2 changed files with 6 additions and 8 deletions

View File

@ -1,13 +1,12 @@
// option2.rs // option2.rs
// Make me compile! Execute `rustlings hint option2` for hints // Make me compile! Execute `rustlings hint option2` for hints
// I AM NOT DONE
fn main() { fn main() {
let optional_word = Some(String::from("rustlings")); let optional_word = Some(String::from("rustlings"));
// 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 { if let Some(word) = optional_word { // If you ignore Some() here you'll need to unwrap word
println!("The word is: {}", word); println!("The word is: {}", word); // we don't need to unwrap wrod becasue we're using Some()
} else { } else {
println!("The optional word doesn't contain anything"); println!("The optional word doesn't contain anything");
} }
@ -18,8 +17,8 @@ fn main() {
} }
// 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() { while let Some(integer) = optional_integers_vec.pop() {// So we're wrapping integer in Some(), and this makes the types compatible for the optional integer vector
println!("current value: {}", integer); println!("current value: {}", integer.unwrap()); // Here we need to unwrap the optional integer before printing
} }
} }

View File

@ -1,7 +1,6 @@
// option3.rs // option3.rs
// Make me compile! Execute `rustlings hint option3` for hints // Make me compile! Execute `rustlings hint option3` for hints
// I AM NOT DONE
struct Point { struct Point {
x: i32, x: i32,
@ -12,7 +11,7 @@ fn main() {
let y: Option<Point> = Some(Point { x: 100, y: 200 }); let y: Option<Point> = Some(Point { x: 100, y: 200 });
match y { match y {
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y), // It seems that the only issue was that we were referencing y (below) without addressing the fact that it was partially moved?
_ => println!("no match"), _ => println!("no match"),
} }
y; // Fix without deleting this line. y; // Fix without deleting this line.