Skip to content

Commit 4f31cb6

Browse files
committed
Adding options
1 parent a7ac566 commit 4f31cb6

3 files changed

Lines changed: 12 additions & 11 deletions

File tree

exercises/options/options1.rs

Lines changed: 5 additions & 4 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,9 @@ 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 { Some(5) }
16+
else if time_of_day >= 22 && time_of_day < 25 { Some(0) }
17+
else { None }
1718
}
1819

1920
#[cfg(test)]
@@ -33,7 +34,7 @@ mod tests {
3334
fn raw_value() {
3435
// TODO: Fix this test. How do you get at the value contained in the
3536
// Option?
36-
let icecreams = maybe_icecream(12);
37-
assert_eq!(icecreams, 5);
37+
let icecreams = maybe_icecream(12).expect("No icecream");
38+
assert_eq!(icecreams, 5u16);
3839
}
3940
}

exercises/options/options2.rs

Lines changed: 6 additions & 5 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,7 +12,7 @@ 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);
1817
}
1918
}
@@ -32,9 +31,11 @@ mod tests {
3231
// TODO: make this a while let statement - remember that vector.pop also
3332
// adds another layer of Option<T>. You can stack `Option<T>`s into
3433
// while let and if let.
35-
integer = optional_integers.pop() {
36-
assert_eq!(integer, cursor);
37-
cursor -= 1;
34+
while let Some(int) = optional_integers.pop() {
35+
if let Some(integer) = int {
36+
assert_eq!(integer, cursor);
37+
cursor -= 1;
38+
}
3839
}
3940

4041
assert_eq!(cursor, 0);

exercises/options/options3.rs

Lines changed: 1 addition & 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,7 +12,7 @@ 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
}

0 commit comments

Comments
 (0)