mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-27 00:00:03 +03:00
Complete options exercises
This commit is contained in:
parent
ec6bd80f6a
commit
7b3b71242b
|
@ -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 pieces left. At 10PM, someone eats them
|
||||
|
@ -13,7 +12,13 @@ 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 > 23 {
|
||||
None
|
||||
} else if time_of_day >= 22 {
|
||||
Some(0)
|
||||
} else {
|
||||
Some(5)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -34,6 +39,10 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
// Option has two variants: None and Some
|
||||
// Some is a tuple struct that wraps a value with type T
|
||||
// None indicates failure or lack of value
|
|
@ -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]
|
||||
|
@ -13,16 +11,21 @@ mod tests {
|
|||
let optional_target = Some(target);
|
||||
|
||||
// TODO: Make this an if let statement whose value is "Some" type
|
||||
word = optional_target {
|
||||
// This block is checking whether or not optional_target is a typen of Some(T)
|
||||
// Since it is, it's binding the value of Some(target) to word.
|
||||
|
||||
// The result is word = Some(Some(target))
|
||||
if let Some(word) = optional_target {
|
||||
assert_eq!(word, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layered_option() {
|
||||
let range = 10;
|
||||
let mut optional_integers: Vec<Option<i8>> = vec![None];
|
||||
|
||||
// Range is inclusive of the start and exclusive of the end.
|
||||
for i in 1..(range + 1) {
|
||||
optional_integers.push(Some(i));
|
||||
}
|
||||
|
@ -32,7 +35,8 @@ mod tests {
|
|||
// 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() {
|
||||
// Removes the last character for the vector and return it
|
||||
assert_eq!(integer, cursor);
|
||||
cursor -= 1;
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
struct Point {
|
||||
x: i32,
|
||||
|
@ -13,8 +12,9 @@ struct Point {
|
|||
fn main() {
|
||||
let y: Option<Point> = Some(Point { x: 100, y: 200 });
|
||||
|
||||
match y {
|
||||
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
|
||||
match y { // &y can also be used here to borrow instead of moving ownership of y to the match expression
|
||||
// ref is used during pattern matching.
|
||||
Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
|
||||
_ => panic!("no match!"),
|
||||
}
|
||||
y; // Fix without deleting this line.
|
||||
|
|
Loading…
Reference in a new issue