mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-10 12:49:18 +00:00
Complete options exercises
This commit is contained in:
parent
8859a5b724
commit
9159135bdb
@ -1,16 +1,20 @@
|
||||
// options1.rs
|
||||
// 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 pieces left. At 10PM, someone eats them
|
||||
// all, so there'll be no more left :(
|
||||
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
|
||||
// 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 > 24 {
|
||||
None
|
||||
} else if time_of_day >= 22 {
|
||||
Some(0)
|
||||
} else {
|
||||
Some(5)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -28,8 +32,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn raw_value() {
|
||||
// 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).unwrap();
|
||||
assert_eq!(icecreams, 5);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
// options2.rs
|
||||
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
@ -10,8 +8,7 @@ mod tests {
|
||||
let target = "rustlings";
|
||||
let optional_target = Some(target);
|
||||
|
||||
// TODO: Make this an if let statement whose value is "Some" type
|
||||
word = optional_target {
|
||||
if let word = optional_target.unwrap() {
|
||||
assert_eq!(word, target);
|
||||
}
|
||||
}
|
||||
@ -27,9 +24,8 @@ 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() {
|
||||
while let Some(Some(integer)) = optional_integers.pop() {
|
||||
assert_eq!(integer, cursor);
|
||||
cursor -= 1;
|
||||
}
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
// options3.rs
|
||||
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
@ -11,7 +9,7 @@ struct Point {
|
||||
fn main() {
|
||||
let y: Option<Point> = Some(Point { x: 100, y: 200 });
|
||||
|
||||
match y {
|
||||
match &y {
|
||||
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
|
||||
_ => panic!("no match!"),
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user