Skip to content

Commit d660ef9

Browse files
committed
do exec
1 parent 8e5b2da commit d660ef9

File tree

18 files changed

+55
-52
lines changed

18 files changed

+55
-52
lines changed

exercises/error_handling/errors1.rs

Lines changed: 4 additions & 6 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+
fn generate_nametag_text(name: String) -> Result<String, String> {
1513
if name.is_empty() {
16-
// Empty names aren't allowed.
17-
None
14+
// Empty names aren't allowed
15+
Err("`name` was empty; it must be nonempty.".into())
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
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> {
2726
let processing_fee = 1;
2827
let cost_per_item = 5;
29-
let qty = item_quantity.parse::<i32>();
28+
// let qty = item_quantity.parse::<i32>();
29+
let qty = item_quantity.parse::<i32>()?;
3030

3131
Ok(qty * cost_per_item + processing_fee)
3232
}

exercises/error_handling/errors3.rs

Lines changed: 3 additions & 3 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
11-
10+
use std::error::Error;
1211
use std::num::ParseIntError;
1312

14-
fn main() {
13+
fn main() -> Result<(), Box<dyn 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: 6 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,12 @@ 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+
match value {
20+
v if v < 0 => Err(CreationError::Negative),
21+
0 => Err(CreationError::Zero),
22+
_ => Ok(Self(value as u64)),
23+
}
24+
2125
}
2226
}
2327

exercises/error_handling/errors5.rs

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

25-
// I AM NOT DONE
2625

2726
use std::error;
2827
use std::fmt;
2928
use std::num::ParseIntError;
3029

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

exercises/error_handling/errors6.rs

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

12-
// I AM NOT DONE
1312

1413
use std::num::ParseIntError;
1514

@@ -26,13 +25,20 @@ impl ParsePosNonzeroError {
2625
}
2726
// TODO: add another error conversion function here.
2827
// fn from_parseint...
28+
fn from_parse_int(err: ParseIntError) -> Self {
29+
Self::ParseInt(err)
30+
}
2931
}
3032

33+
3134
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
3235
// TODO: change this to return an appropriate error instead of panicking
3336
// when `parse()` returns an error.
34-
let x: i64 = s.parse().unwrap();
35-
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
37+
// let x: i64 = s.parse().unwrap();
38+
// PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
39+
s.parse::<i64>()
40+
.map_err(ParsePosNonzeroError::from_parse_int)
41+
.and_then(|x| PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation))
3642
}
3743

3844
// Don't change anything below this line.

exercises/generics/generics1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
77
// hint.
88

9-
// I AM NOT DONE
109

1110
fn main() {
12-
let mut shopping_list: Vec<?> = Vec::new();
11+
let mut shopping_list: Vec<&'static str> = Vec::new();
1312
shopping_list.push("milk");
1413
}

exercises/generics/generics2.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
77
// hint.
88

9-
// I AM NOT DONE
109

11-
struct Wrapper {
12-
value: u32,
10+
struct Wrapper<T> {
11+
value: T,
1312
}
1413

15-
impl Wrapper {
16-
pub fn new(value: u32) -> Self {
14+
impl<T> Wrapper<T> {
15+
fn new(value: T) -> Self {
1716
Wrapper { value }
1817
}
1918
}

exercises/lifetimes/lifetimes1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
99
// hint.
1010

11-
// I AM NOT DONE
1211

13-
fn longest(x: &str, y: &str) -> &str {
12+
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
1413
if x.len() > y.len() {
1514
x
1615
} else {

exercises/lifetimes/lifetimes2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
77
// hint.
88

9-
// I AM NOT DONE
109

1110
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
1211
if x.len() > y.len() {
@@ -22,6 +21,6 @@ fn main() {
2221
{
2322
let string2 = String::from("xyz");
2423
result = longest(string1.as_str(), string2.as_str());
24+
println!("The longest string is '{}'", result);
2525
}
26-
println!("The longest string is '{}'", result);
2726
}

0 commit comments

Comments
 (0)