Skip to content

Commit 0efe0c4

Browse files
yllmisyllmis
authored andcommitted
update
1 parent a6879eb commit 0efe0c4

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

exercises/options/options1.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
76

87
// This function returns how much icecream there is left in the fridge.
98
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
@@ -13,7 +12,15 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
1312
// value of 0 The Option output should gracefully handle cases where
1413
// time_of_day > 23.
1514
// TODO: Complete the function body - remember to return an Option!
16-
???
15+
if time_of_day < 22 {
16+
Some(5)
17+
} else if time_of_day <= 23 && time_of_day >= 22 {
18+
Some(0)
19+
} else if time_of_day > 23 {
20+
None
21+
} else {
22+
None
23+
}
1724
}
1825

1926
#[cfg(test)]
@@ -34,6 +41,8 @@ mod tests {
3441
// TODO: Fix this test. How do you get at the value contained in the
3542
// Option?
3643
let icecreams = maybe_icecream(12);
37-
assert_eq!(icecreams, 5);
44+
assert_eq!(icecreams, Some(5));
45+
let no_icecreams = maybe_icecream(24);
46+
assert_eq!(no_icecreams, None);
3847
}
3948
}

exercises/options/options2.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
76

87
#[cfg(test)]
98
mod tests {
@@ -13,8 +12,13 @@ mod tests {
1312
let optional_target = Some(target);
1413

1514
// TODO: Make this an if let statement whose value is "Some" type
16-
word = optional_target {
15+
if let Some(word) = optional_target {
1716
assert_eq!(word, target);
17+
if word == "rustlings" {
18+
println!("Yay!");
19+
} else {
20+
println!("Oh no!");
21+
}
1822
}
1923
}
2024

@@ -32,7 +36,7 @@ mod tests {
3236
// TODO: make this a while let statement - remember that vector.pop also
3337
// adds another layer of Option<T>. You can stack `Option<T>`s into
3438
// while let and if let.
35-
integer = optional_integers.pop() {
39+
while let Some(Some(integer)) = optional_integers.pop() {
3640
assert_eq!(integer, cursor);
3741
cursor -= 1;
3842
}

exercises/options/options3.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
76

87
struct Point {
98
x: i32,
@@ -13,9 +12,10 @@ struct Point {
1312
fn main() {
1413
let y: Option<Point> = Some(Point { x: 100, y: 200 });
1514

16-
match y {
15+
match &y {
1716
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
1817
_ => panic!("no match!"),
1918
}
2019
y; // Fix without deleting this line.
20+
2121
}

0 commit comments

Comments
 (0)