mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-07 03:09:19 +00:00
option excersises
This commit is contained in:
parent
ff71b235a3
commit
b428dd2b8d
@ -3,8 +3,6 @@
|
|||||||
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// 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 :(
|
||||||
@ -13,7 +11,13 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
|
|||||||
// value of 0 The Option output should gracefully handle cases where
|
// value of 0 The Option output should gracefully handle cases where
|
||||||
// time_of_day > 23.
|
// time_of_day > 23.
|
||||||
// TODO: Complete the function body - remember to return an Option!
|
// 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 <= 24 {
|
||||||
|
Some(0)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -34,6 +38,6 @@ mod tests {
|
|||||||
// TODO: Fix this test. How do you get at the value contained in the
|
// TODO: Fix this test. How do you get at the value contained in the
|
||||||
// Option?
|
// Option?
|
||||||
let icecreams = maybe_icecream(12);
|
let icecreams = maybe_icecream(12);
|
||||||
assert_eq!(icecreams, 5);
|
assert_eq!(icecreams, Some(5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,8 +3,6 @@
|
|||||||
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
@ -13,7 +11,7 @@ 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 Some(word) = optional_target {
|
||||||
assert_eq!(word, target);
|
assert_eq!(word, target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -32,7 +30,7 @@ mod tests {
|
|||||||
// TODO: make this a while let statement - remember that vector.pop also
|
// 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
|
// adds another layer of Option<T>. You can stack `Option<T>`s into
|
||||||
// while let and if let.
|
// while let and if let.
|
||||||
integer = optional_integers.pop() {
|
while let Some(Some(integer)) = optional_integers.pop() {
|
||||||
assert_eq!(integer, cursor);
|
assert_eq!(integer, cursor);
|
||||||
cursor -= 1;
|
cursor -= 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,8 +3,6 @@
|
|||||||
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
x: i32,
|
x: i32,
|
||||||
y: i32,
|
y: i32,
|
||||||
@ -14,7 +12,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),
|
||||||
_ => panic!("no match!"),
|
_ => panic!("no match!"),
|
||||||
}
|
}
|
||||||
y; // Fix without deleting this line.
|
y; // Fix without deleting this line.
|
||||||
|
|||||||
@ -9,14 +9,12 @@
|
|||||||
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
pub fn generate_nametag_text(name: String) -> Result<String, String> {
|
||||||
|
|
||||||
pub fn generate_nametag_text(name: String) -> Option<String> {
|
|
||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
// Empty names aren't allowed.
|
// Empty names aren't allowed.
|
||||||
None
|
Err("`name` was empty; it must be nonempty.".to_string())
|
||||||
} else {
|
} else {
|
||||||
Some(format!("Hi! My name is {}", name))
|
Ok(format!("Hi! My name is {}", name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user