🦀 Options Solved

This commit is contained in:
asif256000 2023-01-13 17:16:36 -05:00
parent 179bc4ad68
commit f9af8759fd
3 changed files with 15 additions and 13 deletions

View File

@ -1,8 +1,6 @@
// options1.rs // options1.rs
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint. // 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. // This function returns how much icecream there is left in the fridge.
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them // If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
// all, so there'll be no more left :( // all, so there'll be no more left :(
@ -10,7 +8,15 @@
fn maybe_icecream(time_of_day: u16) -> Option<u16> { fn maybe_icecream(time_of_day: u16) -> Option<u16> {
// We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a value of 0 // We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a value of 0
// The Option output should gracefully handle cases where time_of_day > 23. // The Option output should gracefully handle cases where time_of_day > 23.
??? let ice_cream_count = if time_of_day < 22 {
Some(5)
} else if time_of_day < 24 {
Some(0)
} else {
None
};
ice_cream_count
} }
#[cfg(test)] #[cfg(test)]
@ -30,6 +36,6 @@ mod tests {
fn raw_value() { fn raw_value() {
// TODO: Fix this test. How do you get at the value contained in the Option? // TODO: Fix this test. How do you get at the value contained in the Option?
let icecreams = maybe_icecream(12); let icecreams = maybe_icecream(12);
assert_eq!(icecreams, 5); assert_eq!(icecreams, Some(5));
} }
} }

View File

@ -1,8 +1,6 @@
// options2.rs // options2.rs
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -13,8 +11,8 @@ mod tests {
let optional_target = Some(target); let optional_target = Some(target);
// 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_target { if let word = optional_target {
assert_eq!(word, target); assert_eq!(word, Some(target));
} }
} }
@ -28,8 +26,8 @@ mod tests {
// 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.pop() { while let Some(integer) = optional_integers.pop() {
assert_eq!(integer, range); assert_eq!(integer, Some(range));
range -= 1; range -= 1;
} }
} }

View File

@ -1,8 +1,6 @@
// options3.rs // options3.rs
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
struct Point { struct Point {
x: i32, x: i32,
y: i32, y: i32,
@ -12,7 +10,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),
_ => println!("no match"), _ => println!("no match"),
} }
y; // Fix without deleting this line. y; // Fix without deleting this line.