Skip to content

Commit 595ff31

Browse files
author
mutoue
committed
update
1 parent 736ccaa commit 595ff31

File tree

7 files changed

+22
-26
lines changed

7 files changed

+22
-26
lines changed

exercises/error_handling/errors1.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
1010
// hint.
1111

12-
// I AM NOT DONE
13-
14-
pub fn generate_nametag_text(name: String) -> Option<String> {
12+
pub fn generate_nametag_text(name: String) -> Result<String, String> {
1513
if name.is_empty() {
1614
// Empty names aren't allowed.
17-
None
15+
Err("`name` was empty; it must be nonempty.".to_string())
1816
} else {
19-
Some(format!("Hi! My name is {}", name))
17+
Ok(format!("Hi! My name is {}", name))
2018
}
2119
}
2220

exercises/error_handling/errors2.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@
1919
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
2020
// hint.
2121

22-
// I AM NOT DONE
23-
2422
use std::num::ParseIntError;
2523

2624
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
2725
let processing_fee = 1;
2826
let cost_per_item = 5;
29-
let qty = item_quantity.parse::<i32>();
30-
27+
let qty = item_quantity.parse::<i32>()?;
3128
Ok(qty * cost_per_item + processing_fee)
3229
}
3330

exercises/error_handling/errors3.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
11-
1210
use std::num::ParseIntError;
1311

14-
fn main() {
12+
fn main() -> Result<(), ParseIntError> {
1513
let mut tokens = 100;
1614
let pretend_user_input = "8";
1715

@@ -23,6 +21,8 @@ fn main() {
2321
tokens -= cost;
2422
println!("You now have {} tokens.", tokens);
2523
}
24+
25+
Ok(())
2626
}
2727

2828
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {

exercises/error_handling/errors4.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
86
#[derive(PartialEq, Debug)]
97
struct PositiveNonzeroInteger(u64);
108

@@ -17,7 +15,12 @@ enum CreationError {
1715
impl PositiveNonzeroInteger {
1816
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
1917
// Hmm...? Why is this only returning an Ok value?
20-
Ok(PositiveNonzeroInteger(value as u64))
18+
19+
match value {
20+
1.. => Ok(PositiveNonzeroInteger(value as u64)),
21+
0 => Err(CreationError::Zero),
22+
_ => Err(CreationError::Negative),
23+
}
2124
}
2225
}
2326

exercises/options/options1.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
86
// This function returns how much icecream there is left in the fridge.
97
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
108
// all, so there'll be no more left :(
@@ -13,7 +11,11 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
1311
// value of 0 The Option output should gracefully handle cases where
1412
// time_of_day > 23.
1513
// TODO: Complete the function body - remember to return an Option!
16-
???
14+
match time_of_day {
15+
0..22 => Some(5),
16+
22..24 => Some(0),
17+
_ => None,
18+
}
1719
}
1820

1921
#[cfg(test)]
@@ -33,7 +35,7 @@ mod tests {
3335
fn raw_value() {
3436
// TODO: Fix this test. How do you get at the value contained in the
3537
// Option?
36-
let icecreams = maybe_icecream(12);
38+
let icecreams = maybe_icecream(12).unwrap();
3739
assert_eq!(icecreams, 5);
3840
}
3941
}

exercises/options/options2.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
86
#[cfg(test)]
97
mod tests {
108
#[test]
@@ -13,7 +11,7 @@ mod tests {
1311
let optional_target = Some(target);
1412

1513
// TODO: Make this an if let statement whose value is "Some" type
16-
word = optional_target {
14+
if let Some(word) = optional_target {
1715
assert_eq!(word, target);
1816
}
1917
}
@@ -32,7 +30,7 @@ mod tests {
3230
// TODO: make this a while let statement - remember that vector.pop also
3331
// adds another layer of Option<T>. You can stack `Option<T>`s into
3432
// while let and if let.
35-
integer = optional_integers.pop() {
33+
while let Some(Some(integer)) = optional_integers.pop() {
3634
assert_eq!(integer, cursor);
3735
cursor -= 1;
3836
}

exercises/options/options3.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
86
struct Point {
97
x: i32,
108
y: i32,
@@ -14,7 +12,7 @@ fn main() {
1412
let y: Option<Point> = Some(Point { x: 100, y: 200 });
1513

1614
match y {
17-
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
15+
Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
1816
_ => panic!("no match!"),
1917
}
2018
y; // Fix without deleting this line.

0 commit comments

Comments
 (0)