Skip to content

Commit 3b03533

Browse files
committed
Pending changes exported from your codespace
1 parent ade0692 commit 3b03533

File tree

25 files changed

+202
-84
lines changed

25 files changed

+202
-84
lines changed

exercises/enums/enums1.rs

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

5-
// I AM NOT DONE
65

76
#[derive(Debug)]
87
enum Message {
98
// TODO: define a few types of messages as used below
9+
Quit,
10+
Echo,
11+
Move,
12+
ChangeColor,
1013
}
1114

1215
fn main() {

exercises/enums/enums2.rs

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

6-
// I AM NOT DONE
76

87
#[derive(Debug)]
98
enum Message {
109
// TODO: define the different variants used below
10+
Move{ x: i32, y: i32 },
11+
Echo(String),
12+
ChangeColor(i32, i32, i32),
13+
Quit,
1114
}
1215

1316
impl Message {

exercises/enums/enums3.rs

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

8-
// I AM NOT DONE
98

109
enum Message {
1110
// TODO: implement the message variant types based on their usage below
11+
ChangeColor(u8,u8,u8),
12+
Echo(String),
13+
Move(Point),
14+
Quit
1215
}
1316

1417
struct Point {
@@ -43,6 +46,13 @@ impl State {
4346
// variants
4447
// Remember: When passing a tuple as a function argument, you'll need
4548
// extra parentheses: fn function((t, u, p, l, e))
49+
match message{
50+
Message::ChangeColor(r,g,b) => {self.change_color((r,g,b));},//注意,在change_color()的括号里面,还要加个括号,三色的元组
51+
Message::Echo(s) => {self.echo(s);},
52+
Message::Move(p) => {self.move_position(p);},
53+
Message::Quit => {self.quit();},
54+
55+
}
4656
}
4757
}
4858

exercises/error_handling/errors1.rs

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

12-
// I AM NOT DONE
1312

14-
pub fn generate_nametag_text(name: String) -> Option<String> {
13+
pub fn generate_nametag_text(name: String) -> Result<String,String> {
1514
if name.is_empty() {
1615
// Empty names aren't allowed.
17-
None
16+
17+
Err("`name` was empty; it must be nonempty.".to_string())
1818
} else {
19-
Some(format!("Hi! My name is {}", name))
19+
// Some(format!("Hi! My name is {}", name))
20+
Ok(format!("Hi! My name is {}", name))
2021
}
2122
}
2223

exercises/error_handling/errors2.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@
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> {
27-
let processing_fee = 1;
28-
let cost_per_item = 5;
29-
let qty = item_quantity.parse::<i32>();
26+
let qty = item_quantity.parse::<i32>()?;
27+
let processing_fee = 1;
28+
let cost_per_item = 5;
29+
30+
Ok(qty * cost_per_item + processing_fee)
3031

31-
Ok(qty * cost_per_item + processing_fee)
32+
33+
34+
3235
}
3336

3437
#[cfg(test)]

exercises/error_handling/errors3.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@
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

1413
fn main() {
1514
let mut tokens = 100;
1615
let pretend_user_input = "8";
1716

18-
let cost = total_cost(pretend_user_input)?;
17+
let cost = total_cost(pretend_user_input);
1918

20-
if cost > tokens {
19+
if cost.as_ref().unwrap() > &tokens {
2120
println!("You can't afford that many!");
2221
} else {
23-
tokens -= cost;
22+
tokens -= cost.as_ref().unwrap();
2423
println!("You now have {} tokens.", tokens);
2524
}
2625
}

exercises/error_handling/errors4.rs

Lines changed: 8 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,14 @@ 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+
Ok(PositiveNonzeroInteger(value as u64))
21+
}else if value == 0{
22+
Err(CreationError::Zero)
23+
}else{
24+
Err(CreationError::Negative)
25+
}
26+
2127
}
2228
}
2329

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/hashmaps/hashmaps1.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@
1111
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
1212
// hint.
1313

14-
// I AM NOT DONE
14+
1515

1616
use std::collections::HashMap;
1717

1818
fn fruit_basket() -> HashMap<String, u32> {
19-
let mut basket = // TODO: declare your hash map here.
19+
let mut basket = HashMap::new(); //TODO: declare your hash map here.
2020

2121
// Two bananas are already given for you :)
2222
basket.insert(String::from("banana"), 2);
23-
23+
2424
// TODO: Put more fruits in your basket here.
25+
basket.insert(String::from("apple"), 2);
26+
basket.insert(String::from("mango"), 2);
2527

26-
basket
28+
return basket;
2729
}
2830

2931
#[cfg(test)]

exercises/hashmaps/hashmaps2.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
1515
// hint.
1616

17-
// I AM NOT DONE
1817

1918
use std::collections::HashMap;
2019

@@ -40,6 +39,8 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
4039
// TODO: Insert new fruits if they are not already present in the
4140
// basket. Note that you are not allowed to put any type of fruit that's
4241
// already present!
42+
basket.insert(Fruit::Banana,1);
43+
basket.insert(Fruit::Pineapple,1);
4344
}
4445
}
4546

@@ -54,7 +55,7 @@ mod tests {
5455
basket.insert(Fruit::Mango, 2);
5556
basket.insert(Fruit::Lychee, 5);
5657

57-
basket
58+
return basket;
5859
}
5960

6061
#[test]

0 commit comments

Comments
 (0)