mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-01-09 20:03:24 +03:00
Compare commits
No commits in common. "b6c434c445d91a9e886e5639b078635e5eca4eb3" and "190945352a2316154d9856a5d882893326e0136a" have entirely different histories.
b6c434c445
...
190945352a
|
@ -2721,15 +2721,6 @@
|
||||||
"contributions": [
|
"contributions": [
|
||||||
"code"
|
"code"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "loshz",
|
|
||||||
"name": "Dan",
|
|
||||||
"avatar_url": "https://avatars.githubusercontent.com/u/3449337?v=4",
|
|
||||||
"profile": "https://loshz.com",
|
|
||||||
"contributions": [
|
|
||||||
"code"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"contributorsPerLine": 8,
|
"contributorsPerLine": 8,
|
||||||
|
|
|
@ -384,7 +384,6 @@ authors.
|
||||||
<td align="center" valign="top" width="12.5%"><a href="http://wznmickey.com"><img src="https://avatars.githubusercontent.com/u/44784663?v=4?s=100" width="100px;" alt="wznmickey"/><br /><sub><b>wznmickey</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=wznmickey" title="Documentation">📖</a></td>
|
<td align="center" valign="top" width="12.5%"><a href="http://wznmickey.com"><img src="https://avatars.githubusercontent.com/u/44784663?v=4?s=100" width="100px;" alt="wznmickey"/><br /><sub><b>wznmickey</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=wznmickey" title="Documentation">📖</a></td>
|
||||||
<td align="center" valign="top" width="12.5%"><a href="https://github.com/NicolasRoelandt"><img src="https://avatars.githubusercontent.com/u/8594193?v=4?s=100" width="100px;" alt="NicolasRoelandt"/><br /><sub><b>NicolasRoelandt</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=NicolasRoelandt" title="Documentation">📖</a></td>
|
<td align="center" valign="top" width="12.5%"><a href="https://github.com/NicolasRoelandt"><img src="https://avatars.githubusercontent.com/u/8594193?v=4?s=100" width="100px;" alt="NicolasRoelandt"/><br /><sub><b>NicolasRoelandt</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=NicolasRoelandt" title="Documentation">📖</a></td>
|
||||||
<td align="center" valign="top" width="12.5%"><a href="https://github.com/jbouganim-parallel"><img src="https://avatars.githubusercontent.com/u/150748285?v=4?s=100" width="100px;" alt="Josh Bouganim"/><br /><sub><b>Josh Bouganim</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=jbouganim-parallel" title="Code">💻</a></td>
|
<td align="center" valign="top" width="12.5%"><a href="https://github.com/jbouganim-parallel"><img src="https://avatars.githubusercontent.com/u/150748285?v=4?s=100" width="100px;" alt="Josh Bouganim"/><br /><sub><b>Josh Bouganim</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=jbouganim-parallel" title="Code">💻</a></td>
|
||||||
<td align="center" valign="top" width="12.5%"><a href="https://loshz.com"><img src="https://avatars.githubusercontent.com/u/3449337?v=4?s=100" width="100px;" alt="Dan"/><br /><sub><b>Dan</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=loshz" title="Code">💻</a></td>
|
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
Greetings and welcome to `rustlings`. This project contains small exercises to get you used to reading and writing Rust code. This includes reading and responding to compiler messages!
|
Greetings and welcome to `rustlings`. This project contains small exercises to get you used to reading and writing Rust code. This includes reading and responding to compiler messages!
|
||||||
|
|
||||||
|
_...looking for the old, web-based version of Rustlings? Try [here](https://github.com/rust-lang/rustlings/tree/rustlings-1)_
|
||||||
|
|
||||||
Alternatively, for a first-time Rust learner, there are several other resources:
|
Alternatively, for a first-time Rust learner, there are several other resources:
|
||||||
|
|
||||||
- [The Book](https://doc.rust-lang.org/book/index.html) - The most comprehensive resource for learning Rust, but a bit theoretical sometimes. You will be using this along with Rustlings!
|
- [The Book](https://doc.rust-lang.org/book/index.html) - The most comprehensive resource for learning Rust, but a bit theoretical sometimes. You will be using this along with Rustlings!
|
||||||
|
|
|
@ -102,5 +102,6 @@ bin = [
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "rustlings"
|
name = "rustlings"
|
||||||
|
version = "0.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
|
@ -6,11 +6,11 @@
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
// This function returns how much icecream there is left in the fridge.
|
// This function returns how much icecream there is left in the fridge.
|
||||||
// If it's before 10PM, there's 5 scoops left. At 10PM, someone eats it
|
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
|
||||||
// all, so there'll be no more left :(
|
// all, so there'll be no more left :(
|
||||||
fn maybe_icecream(time_of_day: u16) -> Option<u16> {
|
fn maybe_icecream(time_of_day: u16) -> Option<u16> {
|
||||||
// We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a
|
// We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a
|
||||||
// value of 0. The Option output should gracefully handle cases where
|
// value of 0 The Option output should gracefully handle cases where
|
||||||
// time_of_day > 23.
|
// time_of_day > 23.
|
||||||
// TODO: Complete the function body - remember to return an Option!
|
// TODO: Complete the function body - remember to return an Option!
|
||||||
???
|
???
|
||||||
|
@ -22,11 +22,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn check_icecream() {
|
fn check_icecream() {
|
||||||
assert_eq!(maybe_icecream(0), Some(5));
|
|
||||||
assert_eq!(maybe_icecream(9), Some(5));
|
assert_eq!(maybe_icecream(9), Some(5));
|
||||||
assert_eq!(maybe_icecream(18), Some(5));
|
assert_eq!(maybe_icecream(10), Some(5));
|
||||||
assert_eq!(maybe_icecream(22), Some(0));
|
|
||||||
assert_eq!(maybe_icecream(23), Some(0));
|
assert_eq!(maybe_icecream(23), Some(0));
|
||||||
|
assert_eq!(maybe_icecream(22), Some(0));
|
||||||
assert_eq!(maybe_icecream(25), None);
|
assert_eq!(maybe_icecream(25), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,9 +18,7 @@ struct JobStatus {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// TODO: `Arc` isn't enough if you want a **mutable** shared state
|
|
||||||
let status = Arc::new(JobStatus { jobs_completed: 0 });
|
let status = Arc::new(JobStatus { jobs_completed: 0 });
|
||||||
|
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
for _ in 0..10 {
|
for _ in 0..10 {
|
||||||
let status_shared = Arc::clone(&status);
|
let status_shared = Arc::clone(&status);
|
||||||
|
@ -31,12 +29,11 @@ fn main() {
|
||||||
});
|
});
|
||||||
handles.push(handle);
|
handles.push(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Waiting for all jobs to complete
|
|
||||||
for handle in handles {
|
for handle in handles {
|
||||||
handle.join().unwrap();
|
handle.join().unwrap();
|
||||||
|
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice
|
||||||
|
// anything interesting in the output? Do you have to 'join' on all the
|
||||||
|
// handles?
|
||||||
|
println!("jobs completed {}", ???);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Print the value of `JobStatus.jobs_completed`
|
|
||||||
println!("Jobs completed: {}", ???);
|
|
||||||
}
|
}
|
||||||
|
|
20
info.toml
20
info.toml
|
@ -1136,17 +1136,25 @@ to **immutable** data. But we want to *change* the number of `jobs_completed`
|
||||||
so we'll need to also use another type that will only allow one thread to
|
so we'll need to also use another type that will only allow one thread to
|
||||||
mutate the data at a time. Take a look at this section of the book:
|
mutate the data at a time. Take a look at this section of the book:
|
||||||
https://doc.rust-lang.org/book/ch16-03-shared-state.html#atomic-reference-counting-with-arct
|
https://doc.rust-lang.org/book/ch16-03-shared-state.html#atomic-reference-counting-with-arct
|
||||||
|
and keep reading if you'd like more hints :)
|
||||||
|
|
||||||
Keep reading if you'd like more hints :)
|
Do you now have an `Arc` `Mutex` `JobStatus` at the beginning of main? Like:
|
||||||
|
|
||||||
Do you now have an `Arc<Mutex<JobStatus>>` at the beginning of `main`? Like:
|
|
||||||
```
|
```
|
||||||
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
|
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
|
||||||
```
|
```
|
||||||
|
|
||||||
Similar to the code in the following example in the book:
|
Similar to the code in the example in the book that happens after the text
|
||||||
https://doc.rust-lang.org/book/ch16-03-shared-state.html#sharing-a-mutext-between-multiple-threads
|
that says 'Sharing a Mutex<T> Between Multiple Threads'. If not, give that a
|
||||||
"""
|
try! If you do and would like more hints, keep reading!!
|
||||||
|
|
||||||
|
Make sure neither of your threads are holding onto the lock of the mutex
|
||||||
|
while they are sleeping, since this will prevent the other thread from
|
||||||
|
being allowed to get the lock. Locks are automatically released when
|
||||||
|
they go out of scope.
|
||||||
|
|
||||||
|
If you've learned from the sample solutions, I encourage you to come
|
||||||
|
back to this exercise and try it again in a few days to reinforce
|
||||||
|
what you've learned :)"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "threads3"
|
name = "threads3"
|
||||||
|
|
|
@ -49,6 +49,7 @@ bin = [\n",
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "rustlings"
|
name = "rustlings"
|
||||||
|
version = "0.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
publish = false
|
publish = false
|
||||||
"#,
|
"#,
|
||||||
|
|
21
src/init.rs
21
src/init.rs
|
@ -10,24 +10,21 @@ use crate::{embedded::EMBEDDED_FILES, exercise::Exercise};
|
||||||
|
|
||||||
fn create_cargo_toml(exercises: &[Exercise]) -> io::Result<()> {
|
fn create_cargo_toml(exercises: &[Exercise]) -> io::Result<()> {
|
||||||
let mut cargo_toml = Vec::with_capacity(1 << 13);
|
let mut cargo_toml = Vec::with_capacity(1 << 13);
|
||||||
cargo_toml.extend_from_slice(b"bin = [\n");
|
|
||||||
for exercise in exercises {
|
|
||||||
cargo_toml.extend_from_slice(b" { name = \"");
|
|
||||||
cargo_toml.extend_from_slice(exercise.name.as_bytes());
|
|
||||||
cargo_toml.extend_from_slice(b"\", path = \"");
|
|
||||||
cargo_toml.extend_from_slice(exercise.path.to_str().unwrap().as_bytes());
|
|
||||||
cargo_toml.extend_from_slice(b"\" },\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
cargo_toml.extend_from_slice(
|
cargo_toml.extend_from_slice(
|
||||||
br#"]
|
br#"[package]
|
||||||
|
|
||||||
[package]
|
|
||||||
name = "rustlings"
|
name = "rustlings"
|
||||||
|
version = "0.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
publish = false
|
publish = false
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
|
for exercise in exercises {
|
||||||
|
cargo_toml.extend_from_slice(b"\n[[bin]]\nname = \"");
|
||||||
|
cargo_toml.extend_from_slice(exercise.name.as_bytes());
|
||||||
|
cargo_toml.extend_from_slice(b"\"\npath = \"");
|
||||||
|
cargo_toml.extend_from_slice(exercise.path.to_str().unwrap().as_bytes());
|
||||||
|
cargo_toml.extend_from_slice(b"\"\n");
|
||||||
|
}
|
||||||
OpenOptions::new()
|
OpenOptions::new()
|
||||||
.create_new(true)
|
.create_new(true)
|
||||||
.write(true)
|
.write(true)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tests"
|
name = "tests"
|
||||||
|
version = "0.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tests"
|
name = "tests"
|
||||||
|
version = "0.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tests"
|
name = "tests"
|
||||||
|
version = "0.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue