mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 05:09:19 +00:00
finished options
This commit is contained in:
parent
ccd2721335
commit
a58652ac54
@ -1,13 +1,12 @@
|
||||
// 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 {
|
||||
println!("The word is: {}", word);
|
||||
if let Some(word) = optional_word { // If you ignore Some() here you'll need to unwrap word
|
||||
println!("The word is: {}", word); // we don't need to unwrap wrod becasue we're using Some()
|
||||
} else {
|
||||
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>
|
||||
// You can stack `Option<T>`'s into while let and if let
|
||||
integer = optional_integers_vec.pop() {
|
||||
println!("current value: {}", integer);
|
||||
// You can stack `Option<T>`'s into while let and if let`
|
||||
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.unwrap()); // Here we need to unwrap the optional integer before printing
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
// option3.rs
|
||||
// Make me compile! Execute `rustlings hint option3` for hints
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
struct Point {
|
||||
x: i32,
|
||||
@ -12,7 +11,7 @@ fn main() {
|
||||
let y: Option<Point> = Some(Point { x: 100, y: 200 });
|
||||
|
||||
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"),
|
||||
}
|
||||
y; // Fix without deleting this line.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user