Skip to content

Commit

Permalink
options exercise solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrx123 committed Apr 11, 2024
1 parent 918e4bb commit 65c5a64
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
15 changes: 12 additions & 3 deletions exercises/12_options/options1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

// This function returns how much icecream there is left in the fridge.
// If it's before 10PM, there's 5 scoops left. At 10PM, someone eats it
Expand All @@ -13,7 +12,14 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
// value of 0. The Option output should gracefully handle cases where
// time_of_day > 23.
// TODO: Complete the function body - remember to return an Option!
???
if time_of_day < 22 {
Some(5)
} else if time_of_day == 22 || time_of_day <= 23 {
Some(0)
}
else {
None
}
}

#[cfg(test)]
Expand All @@ -35,6 +41,9 @@ mod tests {
// TODO: Fix this test. How do you get at the value contained in the
// Option?
let icecreams = maybe_icecream(12);
assert_eq!(icecreams, 5);
assert_eq!(icecreams.unwrap(), 5);
// if let Some(no) = icecreams {
// assert_eq!(no, 5);
// }
}
}
11 changes: 4 additions & 7 deletions exercises/12_options/options2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[cfg(test)]
mod tests {
#[test]
Expand All @@ -13,7 +11,7 @@ mod tests {
let optional_target = Some(target);

// TODO: Make this an if let statement whose value is "Some" type
word = optional_target {
if let Some(word) = optional_target {
assert_eq!(word, target);
}
}
Expand All @@ -29,10 +27,9 @@ mod tests {

let mut cursor = range;

// 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() {
// 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.

while let Some(Some(integer)) = optional_integers.pop() {
assert_eq!(integer, cursor);
cursor -= 1;
}
Expand Down
4 changes: 1 addition & 3 deletions exercises/12_options/options3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

struct Point {
x: i32,
y: i32,
Expand All @@ -14,7 +12,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),
_ => panic!("no match!"),
}
y; // Fix without deleting this line.
Expand Down

0 comments on commit 65c5a64

Please sign in to comment.