Skip to content

Commit c3e226c

Browse files
committed
update
1 parent a811b4b commit c3e226c

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

exercises/error_handling/errors1.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
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() {
16-
// Empty names aren't allowed.
17-
None
14+
Err(String::from("`name` was empty; it must be nonempty."))
1815
} else {
19-
Some(format!("Hi! My name is {}", name))
16+
Ok(String::from("Hi! My name is ") + &name)
2017
}
2118
}
2219

exercises/error_handling/errors2.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,24 @@
1919
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
2020
// hint.
2121

22-
// I AM NOT DONE
2322

2423
use std::num::ParseIntError;
2524

2625
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
26+
/*
2727
let processing_fee = 1;
2828
let cost_per_item = 5;
29-
let qty = item_quantity.parse::<i32>();
29+
let qty = item_quantity.parse::<i32>()?;
3030
3131
Ok(qty * cost_per_item + processing_fee)
32+
*/
33+
let processing_fee = 1;
34+
let cost_per_item = 5;
35+
let result = item_quantity.parse::<i32>();
36+
match result {
37+
Ok(qty) => Ok(qty * cost_per_item + processing_fee),
38+
Err(e) => Err(e)
39+
}
3240
}
3341

3442
#[cfg(test)]

exercises/error_handling/errors3.rs

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

10-
// I AM NOT DONE
1110

1211
use std::num::ParseIntError;
1312

14-
fn main() {
13+
fn main() -> Result<(), Box<dyn std::error::Error>> {
1514
let mut tokens = 100;
1615
let pretend_user_input = "8";
1716

@@ -23,6 +22,7 @@ fn main() {
2322
tokens -= cost;
2423
println!("You now have {} tokens.", tokens);
2524
}
25+
Ok(())
2626
}
2727

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

exercises/error_handling/errors4.rs

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

6-
// I AM NOT DONE
76

87
#[derive(PartialEq, Debug)]
98
struct PositiveNonzeroInteger(u64);
@@ -17,7 +16,13 @@ enum CreationError {
1716
impl PositiveNonzeroInteger {
1817
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
1918
// Hmm...? Why is this only returning an Ok value?
20-
Ok(PositiveNonzeroInteger(value as u64))
19+
if value < 0 {
20+
Err(CreationError::Negative)
21+
} else if value == 0 {
22+
Err(CreationError::Zero)
23+
} else {
24+
Ok(PositiveNonzeroInteger(value as u64))
25+
}
2126
}
2227
}
2328

exercises/error_handling/errors5.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
2323
// hint.
2424

25-
// I AM NOT DONE
26-
2725
use std::error;
2826
use std::fmt;
2927
use std::num::ParseIntError;
3028

3129
// TODO: update the return type of `main()` to make this compile.
32-
fn main() -> Result<(), Box<dyn ???>> {
30+
fn main() -> Result<(), Box<dyn error::Error>> {
3331
let pretend_user_input = "42";
3432
let x: i64 = pretend_user_input.parse()?;
3533
println!("output={:?}", PositiveNonzeroInteger::new(x)?);

exercises/error_handling/errors6.rs

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

12-
// I AM NOT DONE
13-
1412
use std::num::ParseIntError;
1513

1614
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
@@ -24,14 +22,20 @@ impl ParsePosNonzeroError {
2422
fn from_creation(err: CreationError) -> ParsePosNonzeroError {
2523
ParsePosNonzeroError::Creation(err)
2624
}
27-
// TODO: add another error conversion function here.
28-
// fn from_parseint...
25+
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
26+
ParsePosNonzeroError::ParseInt(err)
27+
}
2928
}
3029

3130
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
3231
// TODO: change this to return an appropriate error instead of panicking
3332
// when `parse()` returns an error.
34-
let x: i64 = s.parse().unwrap();
33+
let x: i64 = match s.parse() {
34+
Ok(x) => x,
35+
Err(e) => {
36+
return Err(ParsePosNonzeroError::from_parseint(e));
37+
}
38+
};
3539
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
3640
}
3741

0 commit comments

Comments
 (0)