Skip to content

Commit 8872546

Browse files
committed
d2
1 parent a5382eb commit 8872546

Some content is hidden

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

47 files changed

+1140
-84
lines changed

.rustlings-state.txt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
DON'T EDIT THIS FILE!
22

3-
move_semantics5
3+
errors4
44

55
intro1
66
intro2
@@ -30,4 +30,28 @@ vecs2
3030
move_semantics1
3131
move_semantics2
3232
move_semantics3
33-
move_semantics4
33+
move_semantics4
34+
move_semantics5
35+
structs1
36+
structs2
37+
structs3
38+
enums1
39+
enums2
40+
enums3
41+
strings1
42+
strings2
43+
strings3
44+
strings4
45+
modules1
46+
modules2
47+
modules3
48+
hashmaps1
49+
hashmaps2
50+
hashmaps3
51+
quiz2
52+
options1
53+
options2
54+
options3
55+
errors1
56+
errors2
57+
errors3

exercises/07_structs/structs1.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
struct ColorRegularStruct {
22
// TODO: Add the fields that the test `regular_structs` expects.
33
// What types should the fields have? What are the minimum and maximum values for RGB colors?
4+
red: u64,
5+
green: u64,
6+
blue: u64,
47
}
58

6-
struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */);
9+
// struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */);
10+
struct ColorTupleStruct(i32, i32, i32);
711

812
#[derive(Debug)]
913
struct UnitStruct;
@@ -19,7 +23,11 @@ mod tests {
1923
#[test]
2024
fn regular_structs() {
2125
// TODO: Instantiate a regular struct.
22-
// let green =
26+
let green = ColorRegularStruct {
27+
red: 0,
28+
green: 255,
29+
blue: 0,
30+
};
2331

2432
assert_eq!(green.red, 0);
2533
assert_eq!(green.green, 255);
@@ -29,7 +37,7 @@ mod tests {
2937
#[test]
3038
fn tuple_structs() {
3139
// TODO: Instantiate a tuple struct.
32-
// let green =
40+
let green = ColorTupleStruct(0, 255, 0);
3341

3442
assert_eq!(green.0, 0);
3543
assert_eq!(green.1, 255);
@@ -39,7 +47,7 @@ mod tests {
3947
#[test]
4048
fn unit_structs() {
4149
// TODO: Instantiate a unit struct.
42-
// let unit_struct =
50+
let unit_struct = UnitStruct;
4351
let message = format!("{unit_struct:?}s are fun!");
4452

4553
assert_eq!(message, "UnitStructs are fun!");

exercises/07_structs/structs2.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ mod tests {
3434
let order_template = create_order_template();
3535

3636
// TODO: Create your own order using the update syntax and template above!
37-
// let your_order =
37+
let your_order = Order {
38+
name: String::from("Hacker in Rust"),
39+
count: 1,
40+
..order_template
41+
};
3842

3943
assert_eq!(your_order.name, "Hacker in Rust");
4044
assert_eq!(your_order.year, order_template.year);

exercises/07_structs/structs3.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ impl Package {
2424
}
2525

2626
// TODO: Add the correct return type to the function signature.
27-
fn is_international(&self) {
27+
fn is_international(&self) -> bool {
2828
// TODO: Read the tests that use this method to find out when a package
2929
// is considered international.
30+
self.recipient_country != self.sender_country
3031
}
3132

3233
// TODO: Add the correct return type to the function signature.
33-
fn get_fees(&self, cents_per_gram: u32) {
34+
fn get_fees(&self, cents_per_gram: u32) -> u32 {
3435
// TODO: Calculate the package's fees.
36+
self.weight_in_grams * cents_per_gram
3537
}
3638
}
3739

exercises/08_enums/enums1.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#[derive(Debug)]
22
enum Message {
33
// TODO: Define a few types of messages as used below.
4+
Resize,
5+
Move,
6+
Echo,
7+
ChangeColor,
8+
Quit
49
}
510

611
fn main() {

exercises/08_enums/enums2.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ struct Point {
77
#[derive(Debug)]
88
enum Message {
99
// TODO: Define the different variants used below.
10+
Resize { width: u32, height: u32 },
11+
Move(Point),
12+
Echo(String),
13+
ChangeColor(u8, u8, u8),
14+
Quit,
1015
}
1116

1217
impl Message {

exercises/08_enums/enums3.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ impl State {
4646
fn process(&mut self, message: Message) {
4747
// TODO: Create a match expression to process the different message
4848
// variants using the methods defined above.
49+
match message {
50+
Message::Resize { width, height } => self.resize(width, height),
51+
Message::Move(p) => self.move_position(p),
52+
Message::Echo(msg) => self.echo(msg),
53+
Message::ChangeColor(r, g, b) => self.change_color(r, g, b),
54+
Message::Quit => self.quit(),
55+
_ => println!("JSR"),
56+
}
4957
}
5058
}
5159

exercises/09_strings/strings1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// TODO: Fix the compiler error without changing the function signature.
22
fn current_favorite_color() -> String {
3-
"blue"
3+
String::from("blue")
44
}
55

66
fn main() {

exercises/09_strings/strings2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn is_a_color_word(attempt: &str) -> bool {
66
fn main() {
77
let word = String::from("green"); // Don't change this line.
88

9-
if is_a_color_word(word) {
9+
if is_a_color_word(&word) {
1010
println!("That is a color word I know!");
1111
} else {
1212
println!("That is not a color word I know.");

exercises/09_strings/strings3.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
fn trim_me(input: &str) -> &str {
22
// TODO: Remove whitespace from both ends of a string.
3+
input.trim()
34
}
45

56
fn compose_me(input: &str) -> String {
67
// TODO: Add " world!" to the string! There are multiple ways to do this.
8+
String::from(input) + " world!"
79
}
810

911
fn replace_me(input: &str) -> String {
1012
// TODO: Replace "cars" in the string with "balloons".
13+
input.replace("cars", "balloons")
1114
}
1215

1316
fn main() {

exercises/09_strings/strings4.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ fn string(arg: String) {
1313
// Your task is to replace `placeholder(…)` with either `string_slice(…)`
1414
// or `string(…)` depending on what you think each value is.
1515
fn main() {
16-
placeholder("blue");
16+
string_slice("blue");
1717

18-
placeholder("red".to_string());
18+
string("red".to_string());
1919

20-
placeholder(String::from("hi"));
20+
string(String::from("hi"));
2121

22-
placeholder("rust is fun!".to_owned());
22+
string("rust is fun!".to_owned());
2323

24-
placeholder("nice weather".into());
24+
string_slice("nice weather".into());
2525

26-
placeholder(format!("Interpolation {}", "Station"));
26+
string(format!("Interpolation {}", "Station"));
2727

2828
// WARNING: This is byte indexing, not character indexing.
2929
// Character indexing can be done using `s.chars().nth(INDEX)`.
30-
placeholder(&String::from("abc")[0..1]);
30+
string_slice(&String::from("abc")[0..1]);
3131

32-
placeholder(" hello there ".trim());
32+
string_slice(" hello there ".trim());
3333

34-
placeholder("Happy Monday!".replace("Mon", "Tues"));
34+
string("Happy Monday!".replace("Mon", "Tues"));
3535

36-
placeholder("mY sHiFt KeY iS sTiCkY".to_lowercase());
36+
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
3737
}

exercises/10_modules/modules1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod sausage_factory {
55
String::from("Ginger")
66
}
77

8-
fn make_sausage() {
8+
pub fn make_sausage() {
99
get_secret_recipe();
1010
println!("sausage!");
1111
}

exercises/10_modules/modules2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
mod delicious_snacks {
55
// TODO: Add the following two `use` statements after fixing them.
6-
// use self::fruits::PEAR as ???;
7-
// use self::veggies::CUCUMBER as ???;
6+
pub use self::fruits::PEAR as fruit;
7+
pub use self::veggies::CUCUMBER as veggie;
88

99
mod fruits {
1010
pub const PEAR: &str = "Pear";

exercises/10_modules/modules3.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
// TODO: Bring `SystemTime` and `UNIX_EPOCH` from the `std::time` module into
55
// your scope. Bonus style points if you can do it with one line!
6-
// use ???;
6+
use std::time::SystemTime;
7+
use std::time::UNIX_EPOCH;
78

89
fn main() {
910
match SystemTime::now().duration_since(UNIX_EPOCH) {

exercises/11_hashmaps/hashmaps1.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ use std::collections::HashMap;
88

99
fn fruit_basket() -> HashMap<String, u32> {
1010
// TODO: Declare the hash map.
11-
// let mut basket =
11+
let mut basket: HashMap<String, u32> = HashMap::new();
1212

1313
// Two bananas are already given for you :)
1414
basket.insert(String::from("banana"), 2);
1515

1616
// TODO: Put more fruits in your basket.
1717

18+
basket.insert(String::from("apple"), 2);
19+
basket.insert(String::from("mango"), 1);
20+
1821
basket
1922
}
2023

exercises/11_hashmaps/hashmaps2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
3232
// TODO: Insert new fruits if they are not already present in the
3333
// basket. Note that you are not allowed to put any type of fruit that's
3434
// already present!
35+
basket.entry(fruit).or_insert(4);
3536
}
3637
}
3738

exercises/11_hashmaps/hashmaps3.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ fn build_scores_table(results: &str) -> HashMap<&str, TeamScores> {
3131
// Keep in mind that goals scored by team 1 will be the number of goals
3232
// conceded by team 2. Similarly, goals scored by team 2 will be the
3333
// number of goals conceded by team 1.
34+
35+
scores
36+
.entry(team_1_name)
37+
.and_modify(|e| {
38+
e.goals_conceded += team_2_score;
39+
e.goals_scored += team_1_score;
40+
})
41+
.or_insert(TeamScores {
42+
goals_scored: team_1_score,
43+
goals_conceded: team_2_score,
44+
});
45+
scores
46+
.entry(team_2_name)
47+
.and_modify(|e| {
48+
e.goals_conceded += team_1_score;
49+
e.goals_scored += team_2_score;
50+
})
51+
.or_insert(TeamScores {
52+
goals_scored: team_2_score,
53+
goals_conceded: team_1_score,
54+
});
3455
}
3556

3657
scores

exercises/12_options/options1.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
// `hour_of_day` is higher than 23.
55
fn maybe_icecream(hour_of_day: u16) -> Option<u16> {
66
// TODO: Complete the function body.
7+
if hour_of_day < 22 {
8+
Some(5)
9+
} else if hour_of_day > 23 {
10+
None
11+
} else {
12+
Some(0)
13+
}
714
}
815

916
fn main() {
@@ -18,8 +25,7 @@ mod tests {
1825
fn raw_value() {
1926
// TODO: Fix this test. How do you get the value contained in the
2027
// Option?
21-
let icecreams = maybe_icecream(12);
22-
28+
let icecreams = maybe_icecream(12).unwrap();
2329
assert_eq!(icecreams, 5); // Don't change this line.
2430
}
2531

exercises/12_options/options2.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ mod tests {
1010
let optional_target = Some(target);
1111

1212
// TODO: Make this an if-let statement whose value is `Some`.
13-
word = optional_target {
13+
// word = optional_target {
14+
// assert_eq!(word, target);
15+
// }
16+
if let Some(word) = optional_target {
1417
assert_eq!(word, target);
1518
}
1619
}
@@ -29,8 +32,9 @@ mod tests {
2932
// TODO: Make this a while-let statement. Remember that `Vec::pop()`
3033
// adds another layer of `Option`. You can do nested pattern matching
3134
// in if-let and while-let statements.
32-
integer = optional_integers.pop() {
35+
while let Some(Some(integer)) = optional_integers.pop() {
3336
assert_eq!(integer, cursor);
37+
println!("JSR");
3438
cursor -= 1;
3539
}
3640

exercises/12_options/options3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99

1010
// TODO: Fix the compiler error by adding something to this match statement.
1111
match optional_point {
12-
Some(p) => println!("Co-ordinates are {},{}", p.x, p.y),
12+
Some(ref p) => println!("Co-ordinates are {},{}", p.x, p.y),
1313
_ => panic!("No match!"),
1414
}
1515

exercises/13_error_handling/errors1.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// construct to `Option` that can be used to express error conditions. Change
55
// the function signature and body to return `Result<String, String>` instead
66
// of `Option<String>`.
7-
fn generate_nametag_text(name: String) -> Option<String> {
7+
fn generate_nametag_text(name: String) -> Result<String, String> {
88
if name.is_empty() {
99
// Empty names aren't allowed
10-
None
10+
Err("Empty names aren't allowed".to_string())
1111
} else {
12-
Some(format!("Hi! My name is {name}"))
12+
Ok(format!("Hi! My name is {name}"))
1313
}
1414
}
1515

0 commit comments

Comments
 (0)