Skip to content

Commit f8472ac

Browse files
committed
update
1 parent 1a3421a commit f8472ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+173
-165
lines changed

exercises/enums/enums1.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
//
33
// No hints this time! ;)
44

5-
// I AM NOT DONE
6-
75
#[derive(Debug)]
86
enum Message {
97
// TODO: define a few types of messages as used below
8+
Quit,
9+
Echo,
10+
Move,
11+
ChangeColor,
1012
}
1113

1214
fn main() {

exercises/enums/enums2.rs

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

6-
// I AM NOT DONE
7-
86
#[derive(Debug)]
97
enum Message {
108
// TODO: define the different variants used below
9+
Move { x: u64, y: u64 },
10+
Echo(String),
11+
ChangeColor(u8, u8, u8),
12+
Quit,
1113
}
1214

1315
impl Message {

exercises/enums/enums3.rs

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

8-
// I AM NOT DONE
9-
108
enum Message {
119
// TODO: implement the message variant types based on their usage below
10+
Move(Point),
11+
Echo(String),
12+
ChangeColor(u8, u8, u8),
13+
Quit,
1214
}
1315

1416
struct Point {
@@ -20,7 +22,7 @@ struct State {
2022
color: (u8, u8, u8),
2123
position: Point,
2224
quit: bool,
23-
message: String
25+
message: String,
2426
}
2527

2628
impl State {
@@ -32,7 +34,9 @@ impl State {
3234
self.quit = true;
3335
}
3436

35-
fn echo(&mut self, s: String) { self.message = s }
37+
fn echo(&mut self, s: String) {
38+
self.message = s
39+
}
3640

3741
fn move_position(&mut self, p: Point) {
3842
self.position = p;
@@ -43,6 +47,12 @@ impl State {
4347
// variants
4448
// Remember: When passing a tuple as a function argument, you'll need
4549
// extra parentheses: fn function((t, u, p, l, e))
50+
match message {
51+
Message::Move(p) => self.move_position(p),
52+
Message::Echo(s) => self.echo(s),
53+
Message::ChangeColor(a, b, c) => self.change_color((a, b, c)),
54+
Message::Quit => self.quit(),
55+
}
4656
}
4757
}
4858

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.".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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +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>();
27+
let qty = item_quantity.parse::<i32>()?;
3028

3129
Ok(qty * cost_per_item + processing_fee)
3230
}

exercises/error_handling/errors3.rs

Lines changed: 2 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,7 @@ fn main() {
2321
tokens -= cost;
2422
println!("You now have {} tokens.", tokens);
2523
}
24+
Ok(())
2625
}
2726

2827
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,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,6 +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?
18+
if value == 0 {
19+
return Err(CreationError::Zero);
20+
}
21+
if value < 0 {
22+
return Err(CreationError::Negative);
23+
}
2024
Ok(PositiveNonzeroInteger(value as u64))
2125
}
2226
}

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: 4 additions & 3 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()`.
@@ -26,12 +24,15 @@ impl ParsePosNonzeroError {
2624
}
2725
// TODO: add another error conversion function here.
2826
// fn from_parseint...
27+
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
28+
ParsePosNonzeroError::ParseInt(err)
29+
}
2930
}
3031

3132
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
3233
// TODO: change this to return an appropriate error instead of panicking
3334
// when `parse()` returns an error.
34-
let x: i64 = s.parse().unwrap();
35+
let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?;
3536
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
3637
}
3738

exercises/generics/generics1.rs

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

9-
// I AM NOT DONE
10-
119
fn main() {
12-
let mut shopping_list: Vec<?> = Vec::new();
10+
let mut shopping_list: Vec<&str> = Vec::new();
1311
shopping_list.push("milk");
1412
}

0 commit comments

Comments
 (0)