Skip to content

Commit

Permalink
52/96
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Schausberger committed Jan 17, 2024
1 parent a48bf34 commit d573b8e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
12 changes: 8 additions & 4 deletions exercises/12_options/options1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +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
// all, so there'll be no more left :(
Expand All @@ -13,7 +11,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 < 22 {
Some(5)
} else if time_of_day >= 22 && time_of_day <= 24 {
Some(0)
} else {
None
}
}

#[cfg(test)]
Expand All @@ -33,7 +37,7 @@ mod tests {
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);
}
}
6 changes: 2 additions & 4 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 word = optional_target.unwrap() {
assert_eq!(word, target);
}
}
Expand All @@ -32,7 +30,7 @@ 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(integer) = optional_integers.pop().flatten() {
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
8 changes: 3 additions & 5 deletions exercises/13_error_handling/errors1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

pub fn generate_nametag_text(name: String) -> Option<String> {
pub fn generate_nametag_text(name: String) -> Result<String, String> {
if name.is_empty() {
// Empty names aren't allowed.
None
Err("`name` was empty; it must be nonempty.".to_string())
} else {
Some(format!("Hi! My name is {}", name))
Ok(format!("Hi! My name is {}", name))
}
}

Expand Down

0 comments on commit d573b8e

Please sign in to comment.