Skip to content

Commit b44472b

Browse files
authored
Merge branch 'main' into comment_cleanup
2 parents 7eef5d1 + 4cfcf2c commit b44472b

File tree

8 files changed

+161
-90
lines changed

8 files changed

+161
-90
lines changed

.all-contributorsrc

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,60 @@
21362136
"contributions": [
21372137
"content"
21382138
]
2139+
},
2140+
{
2141+
"login": "proofconstruction",
2142+
"name": "proofconstruction",
2143+
"avatar_url": "https://avatars.githubusercontent.com/u/74747193?v=4",
2144+
"profile": "https://github.com/proofconstruction",
2145+
"contributions": [
2146+
"infra"
2147+
]
2148+
},
2149+
{
2150+
"login": "IVIURRAY",
2151+
"name": "IVIURRAY",
2152+
"avatar_url": "https://avatars.githubusercontent.com/u/16007179?v=4",
2153+
"profile": "https://www.youtube.com/channel/UCQCjA6qUutAtWqkCA4Z36CQ",
2154+
"contributions": [
2155+
"content"
2156+
]
2157+
},
2158+
{
2159+
"login": "b-apperlo",
2160+
"name": "Bert Apperlo",
2161+
"avatar_url": "https://avatars.githubusercontent.com/u/91734527?v=4",
2162+
"profile": "https://github.com/b-apperlo",
2163+
"contributions": [
2164+
"content"
2165+
]
2166+
},
2167+
{
2168+
"login": "FWDekker",
2169+
"name": "Florine W. Dekker",
2170+
"avatar_url": "https://avatars.githubusercontent.com/u/13442533?v=4",
2171+
"profile": "https://fwdekker.com/",
2172+
"contributions": [
2173+
"content"
2174+
]
2175+
},
2176+
{
2177+
"login": "luhem7",
2178+
"name": "Mehul Gangavelli",
2179+
"avatar_url": "https://avatars.githubusercontent.com/u/4008215?v=4",
2180+
"profile": "https://github.com/luhem7",
2181+
"contributions": [
2182+
"content"
2183+
]
2184+
},
2185+
{
2186+
"login": "Frosthage",
2187+
"name": "Mikael Frosthage",
2188+
"avatar_url": "https://avatars.githubusercontent.com/u/14823314?v=4",
2189+
"profile": "https://github.com/Frosthage",
2190+
"contributions": [
2191+
"content"
2192+
]
21392193
}
21402194
],
21412195
"contributorsPerLine": 8,
@@ -2144,5 +2198,6 @@
21442198
"repoType": "github",
21452199
"repoHost": "https://github.com",
21462200
"skipCi": true,
2147-
"commitConvention": "angular"
2201+
"commitConvention": "angular",
2202+
"commitType": "docs"
21482203
}

.github/workflows/rust.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
runs-on: ubuntu-latest
1515
steps:
1616
- uses: actions/checkout@v3
17+
- name: Fetch & maybe update Cargo.lock
18+
run: cargo fetch --locked
1719
- name: Build
1820
run: cargo build --verbose
1921
- name: Run tests

AUTHORS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ authors.
303303
<tr>
304304
<td align="center" valign="top" width="12.5%"><a href="https://github.com/b1ue64"><img src="https://avatars.githubusercontent.com/u/77976308?v=4?s=100" width="100px;" alt="b1ue64"/><br /><sub><b>b1ue64</b></sub></a><br /><a href="#content-b1ue64" title="Content">🖋</a></td>
305305
<td align="center" valign="top" width="12.5%"><a href="https://github.com/lazywalker"><img src="https://avatars.githubusercontent.com/u/53956?v=4?s=100" width="100px;" alt="lazywalker"/><br /><sub><b>lazywalker</b></sub></a><br /><a href="#content-lazywalker" title="Content">🖋</a></td>
306+
<td align="center" valign="top" width="12.5%"><a href="https://github.com/proofconstruction"><img src="https://avatars.githubusercontent.com/u/74747193?v=4?s=100" width="100px;" alt="proofconstruction"/><br /><sub><b>proofconstruction</b></sub></a><br /><a href="#infra-proofconstruction" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
307+
<td align="center" valign="top" width="12.5%"><a href="https://www.youtube.com/channel/UCQCjA6qUutAtWqkCA4Z36CQ"><img src="https://avatars.githubusercontent.com/u/16007179?v=4?s=100" width="100px;" alt="IVIURRAY"/><br /><sub><b>IVIURRAY</b></sub></a><br /><a href="#content-IVIURRAY" title="Content">🖋</a></td>
308+
<td align="center" valign="top" width="12.5%"><a href="https://github.com/b-apperlo"><img src="https://avatars.githubusercontent.com/u/91734527?v=4?s=100" width="100px;" alt="Bert Apperlo"/><br /><sub><b>Bert Apperlo</b></sub></a><br /><a href="#content-b-apperlo" title="Content">🖋</a></td>
309+
<td align="center" valign="top" width="12.5%"><a href="https://fwdekker.com/"><img src="https://avatars.githubusercontent.com/u/13442533?v=4?s=100" width="100px;" alt="Florine W. Dekker"/><br /><sub><b>Florine W. Dekker</b></sub></a><br /><a href="#content-FWDekker" title="Content">🖋</a></td>
310+
<td align="center" valign="top" width="12.5%"><a href="https://github.com/luhem7"><img src="https://avatars.githubusercontent.com/u/4008215?v=4?s=100" width="100px;" alt="Mehul Gangavelli"/><br /><sub><b>Mehul Gangavelli</b></sub></a><br /><a href="#content-luhem7" title="Content">🖋</a></td>
311+
<td align="center" valign="top" width="12.5%"><a href="https://github.com/Frosthage"><img src="https://avatars.githubusercontent.com/u/14823314?v=4?s=100" width="100px;" alt="Mikael Frosthage"/><br /><sub><b>Mikael Frosthage</b></sub></a><br /><a href="#content-Frosthage" title="Content">🖋</a></td>
306312
</tr>
307313
</tbody>
308314
</table>

exercises/enums/enums3.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct State {
2020
color: (u8, u8, u8),
2121
position: Point,
2222
quit: bool,
23+
message: String
2324
}
2425

2526
impl State {
@@ -31,9 +32,7 @@ impl State {
3132
self.quit = true;
3233
}
3334

34-
fn echo(&self, s: String) {
35-
println!("{}", s);
36-
}
35+
fn echo(&mut self, s: String) { self.message = s }
3736

3837
fn move_position(&mut self, p: Point) {
3938
self.position = p;
@@ -57,6 +56,7 @@ mod tests {
5756
quit: false,
5857
position: Point { x: 0, y: 0 },
5958
color: (0, 0, 0),
59+
message: "hello world".to_string(),
6060
};
6161
state.process(Message::ChangeColor(255, 0, 255));
6262
state.process(Message::Echo(String::from("hello world")));
@@ -67,5 +67,6 @@ mod tests {
6767
assert_eq!(state.position.x, 10);
6868
assert_eq!(state.position.y, 15);
6969
assert_eq!(state.quit, true);
70+
assert_eq!(state.message, "hello world");
7071
}
7172
}

exercises/hashmaps/hashmaps2.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,13 @@ mod tests {
8181
let count = basket.values().sum::<u32>();
8282
assert!(count > 11);
8383
}
84+
85+
#[test]
86+
fn all_fruit_types_in_basket() {
87+
let mut basket = get_fruit_basket();
88+
fruit_basket(&mut basket);
89+
for amount in basket.values() {
90+
assert_ne!(amount, &0);
91+
}
92+
}
8493
}

exercises/hashmaps/hashmaps3.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818

1919
use std::collections::HashMap;
2020

21-
// A structure to store team name and its goal details.
21+
// A structure to store the goal details of a team.
2222
struct Team {
23-
name: String,
2423
goals_scored: u8,
2524
goals_conceded: u8,
2625
}

exercises/move_semantics/move_semantics2.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// move_semantics2.rs
22
//
33
// Expected output:
4-
// vec0 has length 3 content `[22, 44, 66]`
5-
// vec1 has length 4 content `[22, 44, 66, 88]`
4+
// vec0 has length 3, with contents `[22, 44, 66]`
5+
// vec1 has length 4, with contents `[22, 44, 66, 88]`
66
//
77
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
88
// for a hint.
@@ -12,15 +12,13 @@
1212
fn main() {
1313
let vec0 = Vec::new();
1414

15-
// Do not move the following line!
1615
let mut vec1 = fill_vec(vec0);
1716

18-
// Do not change the following line!
19-
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
17+
println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0);
2018

2119
vec1.push(88);
2220

23-
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
21+
println!("{} has length {}, with contents `{:?}`", "vec1", vec1.len(), vec1);
2422
}
2523

2624
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {

info.toml

Lines changed: 79 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ name = "vecs2"
260260
path = "exercises/vecs/vecs2.rs"
261261
mode = "test"
262262
hint = """
263-
Hint 1: `i` is each element from the Vec as they are being iterated. Can you try
264-
multiplying this?
263+
Hint 1: `element` is each element from the Vec as they are being iterated. Can you
264+
try multiplying this?
265265
266266
Hint 2: For the first function, there's a way to directly access the numbers stored
267267
in the Vec, using the * dereference operator. You can both access and write to the
@@ -287,23 +287,24 @@ Also: Try accessing `vec0` after having called `fill_vec()`. See what happens!""
287287
[[exercises]]
288288
name = "move_semantics2"
289289
path = "exercises/move_semantics/move_semantics2.rs"
290-
mode = "compile"
290+
mode = "test"
291291
hint = """
292-
So, `vec0` is passed into the `fill_vec` function as an argument. In Rust,
293-
when an argument is passed to a function and it's not explicitly returned,
294-
you can't use the original variable anymore. We call this "moving" a variable.
295-
Variables that are moved into a function (or block scope) and aren't explicitly
296-
returned get "dropped" at the end of that function. This is also what happens here.
297-
There's a few ways to fix this, try them all if you want:
298-
1. Make another, separate version of the data that's in `vec0` and pass that
292+
When running this exercise for the first time, you'll notice an error about
293+
"borrow of moved value". In Rust, when an argument is passed to a function and
294+
it's not explicitly returned, you can't use the original variable anymore.
295+
We call this "moving" a variable. When we pass `vec0` into `fill_vec`, it's being
296+
"moved" into `vec1`, meaning we can't access `vec0` anymore after the fact.
297+
Rust provides a couple of different ways to mitigate this issue, feel free to try them all:
298+
1. You could make another, separate version of the data that's in `vec0` and pass that
299299
to `fill_vec` instead.
300300
2. Make `fill_vec` borrow its argument instead of taking ownership of it,
301-
and then copy the data within the function in order to return an owned
302-
`Vec<i32>`
303-
3. Make `fill_vec` *mutably* borrow a reference to its argument (which will need to be
304-
mutable), modify it directly, then not return anything. Then you can get rid
305-
of `vec1` entirely -- note that this will change what gets printed by the
306-
first `println!`"""
301+
and then copy the data within the function (`vec.clone()`) in order to return an owned
302+
`Vec<i32>`.
303+
3. Or, you could make `fill_vec` *mutably* borrow a reference to its argument (which will need to be
304+
mutable), modify it directly, then not return anything. This means that `vec0` will change over the
305+
course of the function, and makes `vec1` redundant (make sure to change the parameters of the `println!`
306+
statements if you go this route)
307+
"""
307308

308309
[[exercises]]
309310
name = "move_semantics3"
@@ -905,67 +906,6 @@ The fold method can be useful in the count_collection_iterator function.
905906
For a further challenge, consult the documentation for Iterator to find
906907
a different method that could make your code more compact than using fold."""
907908

908-
# THREADS
909-
910-
[[exercises]]
911-
name = "threads1"
912-
path = "exercises/threads/threads1.rs"
913-
mode = "compile"
914-
hint = """
915-
`JoinHandle` is a struct that is returned from a spawned thread:
916-
https://doc.rust-lang.org/std/thread/fn.spawn.html
917-
918-
A challenge with multi-threaded applications is that the main thread can
919-
finish before the spawned threads are completed.
920-
https://doc.rust-lang.org/book/ch16-01-threads.html#waiting-for-all-threads-to-finish-using-join-handles
921-
922-
Use the JoinHandles to wait for each thread to finish and collect their results.
923-
https://doc.rust-lang.org/std/thread/struct.JoinHandle.html
924-
"""
925-
926-
[[exercises]]
927-
name = "threads2"
928-
path = "exercises/threads/threads2.rs"
929-
mode = "compile"
930-
hint = """
931-
`Arc` is an Atomic Reference Counted pointer that allows safe, shared access
932-
to **immutable** data. But we want to *change* the number of `jobs_completed`
933-
so we'll need to also use another type that will only allow one thread to
934-
mutate the data at a time. Take a look at this section of the book:
935-
https://doc.rust-lang.org/book/ch16-03-shared-state.html#atomic-reference-counting-with-arct
936-
and keep reading if you'd like more hints :)
937-
938-
939-
Do you now have an `Arc` `Mutex` `JobStatus` at the beginning of main? Like:
940-
`let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));`
941-
Similar to the code in the example in the book that happens after the text
942-
that says "We can use Arc<T> to fix this.". If not, give that a try! If you
943-
do and would like more hints, keep reading!!
944-
945-
946-
Make sure neither of your threads are holding onto the lock of the mutex
947-
while they are sleeping, since this will prevent the other thread from
948-
being allowed to get the lock. Locks are automatically released when
949-
they go out of scope.
950-
951-
If you've learned from the sample solutions, I encourage you to come
952-
back to this exercise and try it again in a few days to reinforce
953-
what you've learned :)"""
954-
955-
[[exercises]]
956-
name = "threads3"
957-
path = "exercises/threads/threads3.rs"
958-
mode = "compile"
959-
hint = """
960-
An alternate way to handle concurrency between threads is to use
961-
a mpsc (multiple producer, single consumer) channel to communicate.
962-
With both a sending end and a receiving end, it's possible to
963-
send values in one thread and receive them in another.
964-
Multiple producers are possible by using clone() to create a duplicate
965-
of the original sending end.
966-
See https://doc.rust-lang.org/book/ch16-02-message-passing.html for more info.
967-
"""
968-
969909
# SMART POINTERS
970910

971911
[[exercises]]
@@ -1028,6 +968,67 @@ Check out https://doc.rust-lang.org/std/borrow/enum.Cow.html for documentation
1028968
on the `Cow` type.
1029969
"""
1030970

971+
# THREADS
972+
973+
[[exercises]]
974+
name = "threads1"
975+
path = "exercises/threads/threads1.rs"
976+
mode = "compile"
977+
hint = """
978+
`JoinHandle` is a struct that is returned from a spawned thread:
979+
https://doc.rust-lang.org/std/thread/fn.spawn.html
980+
981+
A challenge with multi-threaded applications is that the main thread can
982+
finish before the spawned threads are completed.
983+
https://doc.rust-lang.org/book/ch16-01-threads.html#waiting-for-all-threads-to-finish-using-join-handles
984+
985+
Use the JoinHandles to wait for each thread to finish and collect their results.
986+
https://doc.rust-lang.org/std/thread/struct.JoinHandle.html
987+
"""
988+
989+
[[exercises]]
990+
name = "threads2"
991+
path = "exercises/threads/threads2.rs"
992+
mode = "compile"
993+
hint = """
994+
`Arc` is an Atomic Reference Counted pointer that allows safe, shared access
995+
to **immutable** data. But we want to *change* the number of `jobs_completed`
996+
so we'll need to also use another type that will only allow one thread to
997+
mutate the data at a time. Take a look at this section of the book:
998+
https://doc.rust-lang.org/book/ch16-03-shared-state.html#atomic-reference-counting-with-arct
999+
and keep reading if you'd like more hints :)
1000+
1001+
1002+
Do you now have an `Arc` `Mutex` `JobStatus` at the beginning of main? Like:
1003+
`let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));`
1004+
Similar to the code in the example in the book that happens after the text
1005+
that says "We can use Arc<T> to fix this.". If not, give that a try! If you
1006+
do and would like more hints, keep reading!!
1007+
1008+
1009+
Make sure neither of your threads are holding onto the lock of the mutex
1010+
while they are sleeping, since this will prevent the other thread from
1011+
being allowed to get the lock. Locks are automatically released when
1012+
they go out of scope.
1013+
1014+
If you've learned from the sample solutions, I encourage you to come
1015+
back to this exercise and try it again in a few days to reinforce
1016+
what you've learned :)"""
1017+
1018+
[[exercises]]
1019+
name = "threads3"
1020+
path = "exercises/threads/threads3.rs"
1021+
mode = "compile"
1022+
hint = """
1023+
An alternate way to handle concurrency between threads is to use
1024+
a mpsc (multiple producer, single consumer) channel to communicate.
1025+
With both a sending end and a receiving end, it's possible to
1026+
send values in one thread and receive them in another.
1027+
Multiple producers are possible by using clone() to create a duplicate
1028+
of the original sending end.
1029+
See https://doc.rust-lang.org/book/ch16-02-message-passing.html for more info.
1030+
"""
1031+
10311032
# MACROS
10321033

10331034
[[exercises]]
@@ -1170,4 +1171,4 @@ name = "as_ref_mut"
11701171
path = "exercises/conversions/as_ref_mut.rs"
11711172
mode = "test"
11721173
hint = """
1173-
Add AsRef<str> as a trait bound to the functions."""
1174+
Add AsRef<str> or AsMut<u32> as a trait bound to the functions."""

0 commit comments

Comments
 (0)