mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-26 00:11:49 +02:00
Compare commits
238 Commits
v6.2.0
...
push-vpkvx
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2b3005670 | ||
|
|
e73fff3bd4 | ||
|
|
8dff0df266 | ||
|
|
5ee7dfb5c2 | ||
|
|
9a3586878d | ||
|
|
a99433c62d | ||
|
|
e76ca5e2b9 | ||
|
|
48bab77609 | ||
|
|
a063bcfb4c | ||
|
|
c5f49cfa48 | ||
|
|
9bcd4198c5 | ||
|
|
29dc8ea9fa | ||
|
|
fa91814aa9 | ||
|
|
0b91db2195 | ||
|
|
7b2d42b0f0 | ||
|
|
bd3bdd620b | ||
|
|
8b4562e102 | ||
|
|
63d8986f2a | ||
|
|
ecaecc2f76 | ||
|
|
78194b4441 | ||
|
|
44699e9b1b | ||
|
|
9978c17d5f | ||
|
|
3cc7e0377c | ||
|
|
d2abc359cc | ||
|
|
7c0d269279 | ||
|
|
8db85946af | ||
|
|
7019f4d178 | ||
|
|
fcd77a83cc | ||
|
|
ae444eb3da | ||
|
|
425c9821e0 | ||
|
|
46c6fb2c82 | ||
|
|
374c3874af | ||
|
|
1eb6c1e469 | ||
|
|
06af3ffc99 | ||
|
|
65dc019fa6 | ||
|
|
a56ccb6f4f | ||
|
|
d9872f2615 | ||
|
|
298be671b9 | ||
|
|
fbfd4f25e7 | ||
|
|
d12735a573 | ||
|
|
1aec7c1152 | ||
|
|
0b55809bb9 | ||
|
|
bde6f7470c | ||
|
|
53ec59ed95 | ||
|
|
ed1ee38923 | ||
|
|
26cf4989a2 | ||
|
|
6e60f441e9 | ||
|
|
d07de879a7 | ||
|
|
dd0634c483 | ||
|
|
fc0cd8f0f8 | ||
|
|
d5cae8ff59 | ||
|
|
38016cb2d6 | ||
|
|
e6cb104294 | ||
|
|
410eb69d25 | ||
|
|
243cf5f261 | ||
|
|
eff2ce8a23 | ||
|
|
fd33c29b26 | ||
|
|
f49164e69b | ||
|
|
9bc7bbe4b4 | ||
|
|
46ad25f925 | ||
|
|
2a725fb137 | ||
|
|
449858655d | ||
|
|
e8c2a79516 | ||
|
|
ea85c1b46e | ||
|
|
6bec6f92c4 | ||
|
|
930a0ea73b | ||
|
|
7e2f56f41a | ||
|
|
e90f5f03f3 | ||
|
|
0e090ae112 | ||
|
|
99496706c5 | ||
|
|
f146553dea | ||
|
|
0432e07864 | ||
|
|
f33ba139b4 | ||
|
|
990a722852 | ||
|
|
a675cb5754 | ||
|
|
baeeff389c | ||
|
|
932bc25d88 | ||
|
|
bdc6dad8de | ||
|
|
ea73af9ba3 | ||
|
|
fc5fc0920f | ||
|
|
9705c161b4 | ||
|
|
8cac21511c | ||
|
|
396ee4d618 | ||
|
|
326169a7fa | ||
|
|
685e069c58 | ||
|
|
84a42a2b24 | ||
|
|
ac6e1b7ce5 | ||
|
|
f516da4138 | ||
|
|
e852e60416 | ||
|
|
bf7d171915 | ||
|
|
d3f819f86f | ||
|
|
aa83fd6bc4 | ||
|
|
e2f7734f37 | ||
|
|
5c17abd1bf | ||
|
|
c52867eb8b | ||
|
|
26fd97a209 | ||
|
|
f0a2cdeb18 | ||
|
|
0c79f2ea3e | ||
|
|
0e9eb9e87e | ||
|
|
0d258b9e96 | ||
|
|
d4fa61e435 | ||
|
|
554301b8e9 | ||
|
|
e3ec0abca4 | ||
|
|
a55e848359 | ||
|
|
2653c3c4d4 | ||
|
|
4e4b65711a | ||
|
|
89c40ba256 | ||
|
|
e56ae6d651 | ||
|
|
64b2f18d92 | ||
|
|
2894f3c45c | ||
|
|
1bae2dcb00 | ||
|
|
b540c6df25 | ||
|
|
8b476e678a | ||
|
|
47f8a0cbe5 | ||
|
|
9459eef032 | ||
|
|
5aaa8924a6 | ||
|
|
4ffce1c297 | ||
|
|
0513660b05 | ||
|
|
3947c4de28 | ||
|
|
664228ef8b | ||
|
|
234a61a3ee | ||
|
|
83d1275d72 | ||
|
|
45abd7d59e | ||
|
|
88e10a9e54 | ||
|
|
1f624d4c2a | ||
|
|
9a25309c1c | ||
|
|
2b7caf6fcb | ||
|
|
938500fd2f | ||
|
|
2d26358602 | ||
|
|
9faa5d3aa4 | ||
|
|
bcc2a136c8 | ||
|
|
dcad002057 | ||
|
|
51b8d2ab25 | ||
|
|
aa3eda70e5 | ||
|
|
2d0860fe1b | ||
|
|
17877366b7 | ||
|
|
5eb3dee59c | ||
|
|
247bd19f93 | ||
|
|
e5ed115288 | ||
|
|
03baa471d9 | ||
|
|
da8b3d143a | ||
|
|
20616ff954 | ||
|
|
f463cf8662 | ||
|
|
e9879eac91 | ||
|
|
47148e78a3 | ||
|
|
fea917c8f2 | ||
|
|
948e16e3c7 | ||
|
|
1e7fc46406 | ||
|
|
71494264ca | ||
|
|
3125561474 | ||
|
|
abf1228a0a | ||
|
|
547a9d947b | ||
|
|
f696d98270 | ||
|
|
44ab7f995d | ||
|
|
92a1214dcd | ||
|
|
388f8da97f | ||
|
|
e96623588c | ||
|
|
e1e316b931 | ||
|
|
c4fd29541b | ||
|
|
a8b13f5a82 | ||
|
|
86fc573d7a | ||
|
|
f82e47f2af | ||
|
|
75a38fa38b | ||
|
|
ac62a3713c | ||
|
|
ea52c99560 | ||
|
|
7d4100ed8a | ||
|
|
c8d1d9c51f | ||
|
|
ab2eb3442e | ||
|
|
dbbeb7d4ed | ||
|
|
bfa00ffbdc | ||
|
|
10eb1a3aee | ||
|
|
fd2bf9f6f6 | ||
|
|
fc1f9f0124 | ||
|
|
789492d1a9 | ||
|
|
afc320bed4 | ||
|
|
cba4a6f9c8 | ||
|
|
5556d42b46 | ||
|
|
7d2bc1c7a4 | ||
|
|
c209c874a9 | ||
|
|
dd52e9cd72 | ||
|
|
0f71a150ff | ||
|
|
74388d4bf4 | ||
|
|
e811dd15b5 | ||
|
|
f22700a4ec | ||
|
|
ee25a7d458 | ||
|
|
594e212b8a | ||
|
|
5c355468c1 | ||
|
|
d1571d18f9 | ||
|
|
cb86b44dea | ||
|
|
833e6e0c92 | ||
|
|
159273e532 | ||
|
|
631f2db1a3 | ||
|
|
a1f0eaab54 | ||
|
|
b1898f6d8b | ||
|
|
d29e9e7e07 | ||
|
|
360605e284 | ||
|
|
64772544fa | ||
|
|
5f4875e2ba | ||
|
|
fd2a8c01cb | ||
|
|
b6129ad081 | ||
|
|
28d0b0a21e | ||
|
|
b779c43126 | ||
|
|
4e12725616 | ||
|
|
570bc9f32d | ||
|
|
47976caa69 | ||
|
|
f1abd8577c | ||
|
|
423b50b068 | ||
|
|
bedf0789f2 | ||
|
|
a2d1cb3b22 | ||
|
|
e7ba88f905 | ||
|
|
50f6e5232e | ||
|
|
8854f0a5ed | ||
|
|
13cc3acdfd | ||
|
|
5b7368c46d | ||
|
|
27999f2d26 | ||
|
|
e74f2a4274 | ||
|
|
d141a73493 | ||
|
|
631f44331e | ||
|
|
b01fddef8b | ||
|
|
78a8553f1c | ||
|
|
b70c1abd7c | ||
|
|
71f31d74bc | ||
|
|
72e557b3a9 | ||
|
|
3eaccbb61a | ||
|
|
b678bd8ed2 | ||
|
|
2baa140615 | ||
|
|
e760f07767 | ||
|
|
ca5d5f0a49 | ||
|
|
69b4fd49fc | ||
|
|
36f315c344 | ||
|
|
8016f5ca2d | ||
|
|
8ef2ff1257 | ||
|
|
6ce31defb6 | ||
|
|
0b3ad9141b | ||
|
|
c903db5c53 | ||
|
|
8a038b946c | ||
|
|
ed9740b72c | ||
|
|
ce3dcc9856 |
2
.cargo/config.toml
Normal file
2
.cargo/config.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[alias]
|
||||
dev = ["run", "--", "dev"]
|
||||
8
.github/workflows/rust.yml
vendored
8
.github/workflows/rust.yml
vendored
@@ -22,22 +22,22 @@ jobs:
|
||||
- uses: DavidAnson/markdownlint-cli2-action@v16
|
||||
with:
|
||||
globs: "exercises/**/*.md"
|
||||
- name: Run cargo fmt
|
||||
- name: rustfmt
|
||||
run: cargo fmt --all --check
|
||||
test:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macOS-latest]
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: swatinem/rust-cache@v2
|
||||
- name: Run cargo test
|
||||
- name: cargo test
|
||||
run: cargo test --workspace
|
||||
dev-check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: swatinem/rust-cache@v2
|
||||
- name: Run rustlings dev check
|
||||
- name: rustlings dev check
|
||||
run: cargo run -- dev check --require-solutions
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[default.extend-words]
|
||||
"earch" = "earch" # Because of <s>earch in the list footer
|
||||
|
||||
[files]
|
||||
extend-exclude = [
|
||||
"CHANGELOG.md",
|
||||
]
|
||||
|
||||
[default.extend-words]
|
||||
"ratatui" = "ratatui"
|
||||
|
||||
83
CHANGELOG.md
83
CHANGELOG.md
@@ -1,3 +1,82 @@
|
||||
## Unreleased
|
||||
|
||||
### Changed
|
||||
|
||||
- Upgrade to Rust edition 2024
|
||||
- Raise the minimum supported Rust version to `1.87`
|
||||
|
||||
<a name="6.4.0"></a>
|
||||
|
||||
## 6.4.0 (2024-11-11)
|
||||
|
||||
### Added
|
||||
|
||||
- The list of exercises is now searchable by pressing `s` or `/` 🔍️ (thanks to [@frroossst](https://github.com/frroossst))
|
||||
- New option `c` in the prompt to manually check all exercises ✅ (thanks to [@Nahor](https://github.com/Nahor))
|
||||
- New command `check-all` to manually check all exercises ✅ (thanks to [@Nahor](https://github.com/Nahor))
|
||||
- Addictive animation for showing the progress of checking all exercises. A nice showcase of parallelism in Rust ✨
|
||||
- New option `x` in the prompt to reset the file of the current exercise 🔄
|
||||
- Allow `dead_code` for all exercises and solutions ⚰️ (thanks to [@huss4in](https://github.com/huss4in))
|
||||
- Pause input while running an exercise to avoid unexpected prompt interactions ⏸️
|
||||
- Limit the maximum number of exercises to 999. Any third-party exercises willing to reach that limit? 🔝
|
||||
|
||||
### Changed
|
||||
|
||||
- `enums3`: Remove redundant enum definition task (thanks to [@senekor](https://github.com/senekor))
|
||||
- `if2`: Make the exercise less confusing by avoiding "fizz", "fuzz", "foo", "bar" and "baz" (thanks to [@senekor](https://github.com/senekor))
|
||||
- `hashmap3`: Use the method `Entry::or_default`.
|
||||
- Update the state of all exercises when checking all of them (thanks to [@Nahor](https://github.com/Nahor))
|
||||
- The main prompt doesn't need a confirmation with ENTER on Unix-like systems anymore.
|
||||
- No more jumping back to a previous exercise when its file is changed. Use the list to jump between exercises.
|
||||
- Dump the solution file after an exercise is done even if the solution's directory doesn't exist.
|
||||
- Rework the footer in the list.
|
||||
- Optimize the file watcher.
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fix bad contrast in the list on terminals with a light theme.
|
||||
|
||||
<a name="6.3.0"></a>
|
||||
|
||||
## 6.3.0 (2024-08-29)
|
||||
|
||||
### Added
|
||||
|
||||
- Add the following exercise lints:
|
||||
- `forbid(unsafe_code)`: You shouldn't write unsafe code in Rustlings.
|
||||
- `forbid(unstable_features)`: You don't need unstable features in Rustlings and shouldn't rely on them while learning Rust.
|
||||
- `forbid(todo)`: You forgot a `todo!()`.
|
||||
- `forbid(empty_loop)`: This can only happen by mistake in Rustlings.
|
||||
- `deny(infinite_loop)`: No infinite loops are needed in Rustlings.
|
||||
- `deny(mem_forget)`: You shouldn't leak memory while still learning Rust.
|
||||
- Show a link to every exercise file in the list.
|
||||
- Add scroll padding in the list.
|
||||
- Break the help footer of the list into two lines when the terminal width isn't big enough.
|
||||
- Enable scrolling with the mouse in the list.
|
||||
- `dev check`: Show the progress of checks.
|
||||
- `dev check`: Check that the length of all exercise names is lower than 32.
|
||||
- `dev check`: Check if exercise contains no tests and isn't marked with `test = false`.
|
||||
|
||||
### Changed
|
||||
|
||||
- The compilation time when installing Rustlings is reduced.
|
||||
- Pressing `c` in the list for "continue on" now quits the list after setting the selected exercise as the current one.
|
||||
- Better highlighting of the solution file after an exercise is done.
|
||||
- Don't show the output of successful tests anymore. Instead, show the pretty output for tests.
|
||||
- Be explicit about `q` only quitting the list and not the whole program in the list.
|
||||
- Be explicit about `r` only resetting one exercise (the selected one) in the list.
|
||||
- Ignore the standard output of `git init`.
|
||||
- `threads3`: Remove the queue length and improve tests.
|
||||
- `errors4`: Use match instead of a comparison chain in the solution.
|
||||
- `functions3`: Only take `u8` to avoid using a too high number of iterations by mistake.
|
||||
- `dev check`: Always check with strict Clippy (warnings to errors) when checking the solutions.
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fix the error on some systems about too many open files during the final check of all exercises.
|
||||
- Fix the list when the terminal height is too low.
|
||||
- Restore the terminal after an error in the list.
|
||||
|
||||
<a name="6.2.0"></a>
|
||||
|
||||
## 6.2.0 (2024-08-09)
|
||||
@@ -72,7 +151,7 @@ You can read about the motivations of this change in [this issue](https://github
|
||||
|
||||
### List mode
|
||||
|
||||
A list mode was added using [Ratatui](https://ratatui.rs).
|
||||
A new list mode was added!
|
||||
You can enter it by entering `l` in the watch mode.
|
||||
It offers the following features:
|
||||
|
||||
@@ -773,7 +852,7 @@ Then follow the link to the guide about [third-party exercises](THIRD_PARTY_EXER
|
||||
|
||||
#### Bug Fixes
|
||||
|
||||
- Update deps to version compatable with aarch64-pc-windows (#263) ([19a93428](https://github.com/rust-lang/rustlings/commit/19a93428b3c73d994292671f829bdc8e5b7b3401))
|
||||
- Update deps to version compatible with aarch64-pc-windows (#263) ([19a93428](https://github.com/rust-lang/rustlings/commit/19a93428b3c73d994292671f829bdc8e5b7b3401))
|
||||
- **docs:**
|
||||
- Added a necessary step to Windows installation process (#242) ([3906efcd](https://github.com/rust-lang/rustlings/commit/3906efcd52a004047b460ed548037093de3f523f))
|
||||
- Fixed mangled sentence from book; edited for clarity (#266) ([ade52ff](https://github.com/rust-lang/rustlings/commit/ade52ffb739987287ddd5705944c8777705faed9))
|
||||
|
||||
623
Cargo.lock
generated
623
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
48
Cargo.toml
48
Cargo.toml
@@ -6,7 +6,7 @@ exclude = [
|
||||
]
|
||||
|
||||
[workspace.package]
|
||||
version = "6.2.0"
|
||||
version = "6.4.0"
|
||||
authors = [
|
||||
"Mo Bitar <mo8it@proton.me>", # https://github.com/mo8it
|
||||
"Liv <mokou@fastmail.com>", # https://github.com/shadows-withal
|
||||
@@ -15,12 +15,12 @@ authors = [
|
||||
]
|
||||
repository = "https://github.com/rust-lang/rustlings"
|
||||
license = "MIT"
|
||||
edition = "2021" # On Update: Update the edition of the `rustfmt` command that checks the solutions.
|
||||
rust-version = "1.80"
|
||||
edition = "2024" # On Update: Update the edition of `rustfmt` in `dev check` and `CARGO_TOML` in `dev new`.
|
||||
rust-version = "1.87"
|
||||
|
||||
[workspace.dependencies]
|
||||
serde = { version = "1.0.205", features = ["derive"] }
|
||||
toml_edit = { version = "0.22.20", default-features = false, features = ["parse", "serde"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
toml_edit = { version = "0.22", default-features = false, features = ["parse", "serde"] }
|
||||
|
||||
[package]
|
||||
name = "rustlings"
|
||||
@@ -46,19 +46,20 @@ include = [
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
ahash = { version = "0.8.11", default-features = false }
|
||||
anyhow = "1.0.86"
|
||||
clap = { version = "4.5.14", features = ["derive"] }
|
||||
notify-debouncer-mini = { version = "0.4.1", default-features = false }
|
||||
os_pipe = "1.2.1"
|
||||
ratatui = { version = "0.28.0", default-features = false, features = ["crossterm"] }
|
||||
rustlings-macros = { path = "rustlings-macros", version = "=6.2.0" }
|
||||
serde_json = "1.0.122"
|
||||
anyhow = "1.0"
|
||||
clap = { version = "4.5", features = ["derive"] }
|
||||
crossterm = { version = "0.29", default-features = false, features = ["windows", "events"] }
|
||||
notify = "8.0"
|
||||
rustlings-macros = { path = "rustlings-macros", version = "=6.4.0" }
|
||||
serde_json = "1.0"
|
||||
serde.workspace = true
|
||||
toml_edit.workspace = true
|
||||
|
||||
[target.'cfg(not(windows))'.dependencies]
|
||||
rustix = { version = "1.0", default-features = false, features = ["std", "stdio", "termios"] }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.12.0"
|
||||
tempfile = "3.19"
|
||||
|
||||
[profile.release]
|
||||
panic = "abort"
|
||||
@@ -68,7 +69,20 @@ panic = "abort"
|
||||
|
||||
[package.metadata.release]
|
||||
pre-release-hook = ["./release-hook.sh"]
|
||||
pre-release-commit-message = "Release 🎉"
|
||||
|
||||
# TODO: Remove after the following fix is released: https://github.com/rust-lang/rust-clippy/pull/13102
|
||||
[lints.clippy]
|
||||
needless_option_as_deref = "allow"
|
||||
[workspace.lints.rust]
|
||||
unsafe_code = "forbid"
|
||||
unstable_features = "forbid"
|
||||
|
||||
[workspace.lints.clippy]
|
||||
empty_loop = "forbid"
|
||||
disallowed-types = "deny"
|
||||
disallowed-methods = "deny"
|
||||
infinite_loop = "deny"
|
||||
mem_forget = "deny"
|
||||
dbg_macro = "warn"
|
||||
todo = "warn"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
39
README.md
39
README.md
@@ -21,12 +21,13 @@ Before installing Rustlings, you need to have the **latest version of Rust** ins
|
||||
Visit [www.rust-lang.org/tools/install](https://www.rust-lang.org/tools/install) for further instructions on installing Rust.
|
||||
This will also install _Cargo_, Rust's package/project manager.
|
||||
|
||||
> 🐧 If you're on Linux, make sure you've installed `gcc` (for a linker).
|
||||
> 🐧 If you are on Linux, make sure you have installed `gcc` (for a linker).
|
||||
>
|
||||
> Deb: `sudo apt install gcc`.
|
||||
> Dnf: `sudo dnf install gcc`.
|
||||
> Deb: `sudo apt install gcc`
|
||||
>
|
||||
> Dnf: `sudo dnf install gcc`
|
||||
|
||||
> 🍎 If you're on MacOS, make sure you've installed Xcode and its developer tools by running `xcode-select --install`.
|
||||
> 🍎 If you are on MacOS, make sure you have installed Xcode and its developer tools by running `xcode-select --install`.
|
||||
|
||||
### Installing Rustlings
|
||||
|
||||
@@ -102,7 +103,7 @@ Ask for hints by entering `h` in the _watch mode_ 💡
|
||||
|
||||
### Watch Mode
|
||||
|
||||
After [initialization](#initialization), Rustlings can be launched by simply running the command `rustlings`.
|
||||
After the [initialization](#initialization), Rustlings can be launched by simply running the command `rustlings`.
|
||||
|
||||
This will start the _watch mode_ which walks you through the exercises in a predefined order (what we think is best for newcomers).
|
||||
It will rerun the current exercise automatically every time you change the exercise's file in the `exercises/` directory.
|
||||
@@ -124,21 +125,31 @@ The list allows you to…
|
||||
|
||||
- See the status of all exercises (done or pending)
|
||||
- `c`: Continue at another exercise (temporarily skip some exercises or go back to a previous one)
|
||||
- `r`: Reset status and file of an exercise (you need to _reload/reopen_ its file in your editor afterwards)
|
||||
- `r`: Reset status and file of the selected exercise (you need to _reload/reopen_ its file in your editor afterwards)
|
||||
|
||||
See the footer of the list for all possible keys.
|
||||
|
||||
## Questions?
|
||||
|
||||
If you need any help while doing the exercises and the builtin-hints aren't helpful, feel free to ask in the [_Q&A_ category of the discussions](https://github.com/rust-lang/rustlings/discussions/categories/q-a?discussions_q=) if your question wasn't asked yet 💡
|
||||
|
||||
## Third-Party Exercises
|
||||
|
||||
Third-party exercises are a set of exercises maintained by the community.
|
||||
You can use the same `rustlings` program that you installed with `cargo install rustlings` to run them:
|
||||
|
||||
- 🇯🇵 [Japanese Rustlings](https://github.com/sotanengel/rustlings-jp):A Japanese translation of the Rustlings exercises.
|
||||
- 🇨🇳 [Simplified Chinese Rustlings](https://github.com/SandmeyerX/rustlings-zh-cn): A simplified Chinese translation of the Rustlings exercises.
|
||||
|
||||
Do you want to create your own set of Rustlings exercises to focus on some specific topic?
|
||||
Or do you want to translate the original Rustlings exercises?
|
||||
Then follow the the guide about [third-party exercises](https://github.com/rust-lang/rustlings/blob/main/THIRD_PARTY_EXERCISES.md)!
|
||||
|
||||
## Continuing On
|
||||
|
||||
Once you've completed Rustlings, put your new knowledge to good use!
|
||||
Continue practicing your Rust skills by building your own projects, contributing to Rustlings, or finding other open-source projects to contribute to.
|
||||
|
||||
## Third-Party Exercises
|
||||
|
||||
Do you want to create your own set of Rustlings exercises to focus on some specific topic?
|
||||
Or do you want to translate the original Rustlings exercises?
|
||||
Then follow the link to the guide about [third-party exercises](https://github.com/rust-lang/rustlings/blob/main/THIRD_PARTY_EXERCISES.md)!
|
||||
|
||||
## Uninstalling Rustlings
|
||||
|
||||
If you want to remove Rustlings from your system, run the following command:
|
||||
@@ -151,6 +162,4 @@ cargo uninstall rustlings
|
||||
|
||||
See [CONTRIBUTING.md](https://github.com/rust-lang/rustlings/blob/main/CONTRIBUTING.md) 🔗
|
||||
|
||||
## Contributors ✨
|
||||
|
||||
Thanks to [all the wonderful contributors](https://github.com/rust-lang/rustlings/graphs/contributors) 🎉
|
||||
Thanks to [all the wonderful contributors](https://github.com/rust-lang/rustlings/graphs/contributors) ✨
|
||||
|
||||
5
build.rs
Normal file
5
build.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
// Fix building from source on Windows because it can't handle file links.
|
||||
#[cfg(windows)]
|
||||
let _ = std::fs::copy("dev/Cargo.toml", "dev-Cargo.toml");
|
||||
}
|
||||
15
clippy.toml
Normal file
15
clippy.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
disallowed-types = [
|
||||
# Inefficient. Use `.queue(…)` instead.
|
||||
"crossterm::style::Stylize",
|
||||
"crossterm::style::styled_content::StyledContent",
|
||||
]
|
||||
|
||||
disallowed-methods = [
|
||||
# Inefficient. Use `.queue(…)` instead.
|
||||
"crossterm::style::style",
|
||||
# Use `thread::Builder::spawn` instead and handle the error.
|
||||
"std::thread::spawn",
|
||||
"std::thread::Scope::spawn",
|
||||
# Return `ExitCode` instead.
|
||||
"std::process::exit",
|
||||
]
|
||||
@@ -1,4 +1,4 @@
|
||||
# Don't edit the `bin` list manually! It is updated by `cargo run -- dev update`. This comment line will be stripped in `rustlings init`.
|
||||
# Don't edit the `bin` list manually! It is updated by `cargo dev update`. This comment line will be stripped in `rustlings init`.
|
||||
bin = [
|
||||
{ name = "intro1", path = "../exercises/00_intro/intro1.rs" },
|
||||
{ name = "intro1_sol", path = "../solutions/00_intro/intro1.rs" },
|
||||
@@ -164,40 +164,67 @@ bin = [
|
||||
{ name = "threads2_sol", path = "../solutions/20_threads/threads2.rs" },
|
||||
{ name = "threads3", path = "../exercises/20_threads/threads3.rs" },
|
||||
{ name = "threads3_sol", path = "../solutions/20_threads/threads3.rs" },
|
||||
{ name = "macros1", path = "../exercises/21_macros/macros1.rs" },
|
||||
{ name = "macros1_sol", path = "../solutions/21_macros/macros1.rs" },
|
||||
{ name = "macros2", path = "../exercises/21_macros/macros2.rs" },
|
||||
{ name = "macros2_sol", path = "../solutions/21_macros/macros2.rs" },
|
||||
{ name = "macros3", path = "../exercises/21_macros/macros3.rs" },
|
||||
{ name = "macros3_sol", path = "../solutions/21_macros/macros3.rs" },
|
||||
{ name = "macros4", path = "../exercises/21_macros/macros4.rs" },
|
||||
{ name = "macros4_sol", path = "../solutions/21_macros/macros4.rs" },
|
||||
{ name = "clippy1", path = "../exercises/22_clippy/clippy1.rs" },
|
||||
{ name = "clippy1_sol", path = "../solutions/22_clippy/clippy1.rs" },
|
||||
{ name = "clippy2", path = "../exercises/22_clippy/clippy2.rs" },
|
||||
{ name = "clippy2_sol", path = "../solutions/22_clippy/clippy2.rs" },
|
||||
{ name = "clippy3", path = "../exercises/22_clippy/clippy3.rs" },
|
||||
{ name = "clippy3_sol", path = "../solutions/22_clippy/clippy3.rs" },
|
||||
{ name = "using_as", path = "../exercises/23_conversions/using_as.rs" },
|
||||
{ name = "using_as_sol", path = "../solutions/23_conversions/using_as.rs" },
|
||||
{ name = "from_into", path = "../exercises/23_conversions/from_into.rs" },
|
||||
{ name = "from_into_sol", path = "../solutions/23_conversions/from_into.rs" },
|
||||
{ name = "from_str", path = "../exercises/23_conversions/from_str.rs" },
|
||||
{ name = "from_str_sol", path = "../solutions/23_conversions/from_str.rs" },
|
||||
{ name = "try_from_into", path = "../exercises/23_conversions/try_from_into.rs" },
|
||||
{ name = "try_from_into_sol", path = "../solutions/23_conversions/try_from_into.rs" },
|
||||
{ name = "as_ref_mut", path = "../exercises/23_conversions/as_ref_mut.rs" },
|
||||
{ name = "as_ref_mut_sol", path = "../solutions/23_conversions/as_ref_mut.rs" },
|
||||
{ name = "async1", path = "../exercises/21_async/async1.rs" },
|
||||
{ name = "async1_sol", path = "../solutions/21_async/async1.rs" },
|
||||
{ name = "async2", path = "../exercises/21_async/async2.rs" },
|
||||
{ name = "async2_sol", path = "../solutions/21_async/async2.rs" },
|
||||
{ name = "macros1", path = "../exercises/22_macros/macros1.rs" },
|
||||
{ name = "macros1_sol", path = "../solutions/22_macros/macros1.rs" },
|
||||
{ name = "macros2", path = "../exercises/22_macros/macros2.rs" },
|
||||
{ name = "macros2_sol", path = "../solutions/22_macros/macros2.rs" },
|
||||
{ name = "macros3", path = "../exercises/22_macros/macros3.rs" },
|
||||
{ name = "macros3_sol", path = "../solutions/22_macros/macros3.rs" },
|
||||
{ name = "macros4", path = "../exercises/22_macros/macros4.rs" },
|
||||
{ name = "macros4_sol", path = "../solutions/22_macros/macros4.rs" },
|
||||
{ name = "clippy1", path = "../exercises/23_clippy/clippy1.rs" },
|
||||
{ name = "clippy1_sol", path = "../solutions/23_clippy/clippy1.rs" },
|
||||
{ name = "clippy2", path = "../exercises/23_clippy/clippy2.rs" },
|
||||
{ name = "clippy2_sol", path = "../solutions/23_clippy/clippy2.rs" },
|
||||
{ name = "clippy3", path = "../exercises/23_clippy/clippy3.rs" },
|
||||
{ name = "clippy3_sol", path = "../solutions/23_clippy/clippy3.rs" },
|
||||
{ name = "using_as", path = "../exercises/24_conversions/using_as.rs" },
|
||||
{ name = "using_as_sol", path = "../solutions/24_conversions/using_as.rs" },
|
||||
{ name = "from_into", path = "../exercises/24_conversions/from_into.rs" },
|
||||
{ name = "from_into_sol", path = "../solutions/24_conversions/from_into.rs" },
|
||||
{ name = "from_str", path = "../exercises/24_conversions/from_str.rs" },
|
||||
{ name = "from_str_sol", path = "../solutions/24_conversions/from_str.rs" },
|
||||
{ name = "try_from_into", path = "../exercises/24_conversions/try_from_into.rs" },
|
||||
{ name = "try_from_into_sol", path = "../solutions/24_conversions/try_from_into.rs" },
|
||||
{ name = "as_ref_mut", path = "../exercises/24_conversions/as_ref_mut.rs" },
|
||||
{ name = "as_ref_mut_sol", path = "../solutions/24_conversions/as_ref_mut.rs" },
|
||||
]
|
||||
|
||||
[package]
|
||||
name = "exercises"
|
||||
edition = "2021"
|
||||
edition = "2024"
|
||||
# Don't publish the exercises on crates.io!
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1.45.0", features = ["rt-multi-thread", "macros"] }
|
||||
|
||||
[profile.release]
|
||||
panic = "abort"
|
||||
|
||||
[profile.dev]
|
||||
panic = "abort"
|
||||
|
||||
[lints.rust]
|
||||
# You shouldn't write unsafe code in Rustlings!
|
||||
unsafe_code = "forbid"
|
||||
# You don't need unstable features in Rustlings and shouldn't rely on them while learning Rust.
|
||||
unstable_features = "forbid"
|
||||
# Dead code warnings can't be avoided in some exercises and might distract while learning.
|
||||
dead_code = "allow"
|
||||
|
||||
[lints.clippy]
|
||||
# You forgot a `todo!()`!
|
||||
todo = "forbid"
|
||||
# This can only happen by mistake in Rustlings.
|
||||
empty_loop = "forbid"
|
||||
# No infinite loops are needed in Rustlings.
|
||||
infinite_loop = "deny"
|
||||
# You shouldn't leak memory while still learning Rust!
|
||||
mem_forget = "deny"
|
||||
# Currently, there are no disallowed methods. This line avoids problems when developing Rustlings.
|
||||
disallowed_methods = "allow"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// TODO: We sometimes encourage you to keep trying things on a given exercise,
|
||||
// TODO: We sometimes encourage you to keep trying things on a given exercise
|
||||
// even after you already figured it out. If you got everything working and feel
|
||||
// ready for the next exercise, enter `n` in the terminal.
|
||||
//
|
||||
@@ -6,8 +6,7 @@
|
||||
// Try adding a new `println!` and check the updated output in the terminal.
|
||||
|
||||
fn main() {
|
||||
println!("Hello and");
|
||||
println!(r#" welcome to... "#);
|
||||
println!(r#" Welcome to... "#);
|
||||
println!(r#" _ _ _ "#);
|
||||
println!(r#" _ __ _ _ ___| |_| (_)_ __ __ _ ___ "#);
|
||||
println!(r#" | '__| | | / __| __| | | '_ \ / _` / __| "#);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Variables
|
||||
|
||||
In Rust, variables are immutable by default.
|
||||
When a variable is immutable, once a value is bound to a name, you can’t change that value.
|
||||
When a variable is immutable, once a value is bound to a name, you can't change that value.
|
||||
You can make them mutable by adding `mut` in front of the variable name.
|
||||
|
||||
## Further information
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
fn main() {
|
||||
// TODO: Add missing keyword.
|
||||
// TODO: Add the missing keyword.
|
||||
x = 5;
|
||||
|
||||
println!("x has the value {x}");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
fn main() {
|
||||
let number = "T-H-R-E-E"; // Don't change this line
|
||||
println!("Spell a number: {}", number);
|
||||
println!("Spell a number: {number}");
|
||||
|
||||
// TODO: Fix the compiler error by changing the line below without renaming the variable.
|
||||
number = 3;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
fn call_me(num: u32) {
|
||||
fn call_me(num: u8) {
|
||||
for i in 0..num {
|
||||
println!("Ring! Call number {}", i + 1);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// TODO: Fix the compiler error on this function.
|
||||
fn foo_if_fizz(fizzish: &str) -> &str {
|
||||
if fizzish == "fizz" {
|
||||
"foo"
|
||||
fn picky_eater(food: &str) -> &str {
|
||||
if food == "strawberry" {
|
||||
"Yummy!"
|
||||
} else {
|
||||
1
|
||||
}
|
||||
@@ -18,18 +18,20 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn foo_for_fizz() {
|
||||
// This means that calling `foo_if_fizz` with the argument "fizz" should return "foo".
|
||||
assert_eq!(foo_if_fizz("fizz"), "foo");
|
||||
fn yummy_food() {
|
||||
// This means that calling `picky_eater` with the argument "strawberry" should return "Yummy!".
|
||||
assert_eq!(picky_eater("strawberry"), "Yummy!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bar_for_fuzz() {
|
||||
assert_eq!(foo_if_fizz("fuzz"), "bar");
|
||||
fn neutral_food() {
|
||||
assert_eq!(picky_eater("potato"), "I guess I can eat that.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_to_baz() {
|
||||
assert_eq!(foo_if_fizz("literally anything"), "baz");
|
||||
fn default_disliked_food() {
|
||||
assert_eq!(picky_eater("broccoli"), "No thanks!");
|
||||
assert_eq!(picky_eater("gummy bears"), "No thanks!");
|
||||
assert_eq!(picky_eater("literally anything"), "No thanks!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# Enums
|
||||
|
||||
Rust allows you to define types called "enums" which enumerate possible values.
|
||||
Enums are a feature in many languages, but their capabilities differ in each language. Rust’s enums are most similar to algebraic data types in functional languages, such as F#, OCaml, and Haskell.
|
||||
Enums are a feature in many languages, but their capabilities differ in each language. Rust's enums are most similar to algebraic data types in functional languages, such as F#, OCaml, and Haskell.
|
||||
Useful in combination with enums is Rust's "pattern matching" facility, which makes it easy to run different code for different values of an enumeration.
|
||||
|
||||
## Further information
|
||||
|
||||
- [Enums](https://doc.rust-lang.org/book/ch06-00-enums.html)
|
||||
- [Pattern syntax](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html)
|
||||
- [Pattern syntax](https://doc.rust-lang.org/book/ch19-03-pattern-syntax.html)
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Point {
|
||||
x: u64,
|
||||
|
||||
@@ -4,7 +4,11 @@ struct Point {
|
||||
}
|
||||
|
||||
enum Message {
|
||||
// TODO: Implement the message variant types based on their usage below.
|
||||
Resize { width: u64, height: u64 },
|
||||
Move(Point),
|
||||
Echo(String),
|
||||
ChangeColor(u8, u8, u8),
|
||||
Quit,
|
||||
}
|
||||
|
||||
struct State {
|
||||
|
||||
@@ -23,6 +23,7 @@ mod tests {
|
||||
assert_eq!(trim_me("Hello! "), "Hello!");
|
||||
assert_eq!(trim_me(" What's up!"), "What's up!");
|
||||
assert_eq!(trim_me(" Hola! "), "Hola!");
|
||||
assert_eq!(trim_me("Hi!"), "Hi!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// You can bring module paths into scopes and provide new names for them with
|
||||
// the `use` and `as` keywords.
|
||||
|
||||
#[allow(dead_code)]
|
||||
mod delicious_snacks {
|
||||
// TODO: Add the following two `use` statements after fixing them.
|
||||
// use self::fruits::PEAR as ???;
|
||||
|
||||
@@ -17,7 +17,7 @@ struct TeamScores {
|
||||
|
||||
fn build_scores_table(results: &str) -> HashMap<&str, TeamScores> {
|
||||
// The name of the team is the key and its associated struct is the value.
|
||||
let mut scores = HashMap::new();
|
||||
let mut scores = HashMap::<&str, TeamScores>::new();
|
||||
|
||||
for line in results.lines() {
|
||||
let mut split_iterator = line.split(',');
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// This function returns how much icecream there is left in the fridge.
|
||||
// This function returns how much ice cream there is left in the fridge.
|
||||
// If it's before 22:00 (24-hour system), then 5 scoops are left. At 22:00,
|
||||
// someone eats it all, so no icecream is left (value 0). Return `None` if
|
||||
// someone eats it all, so no ice cream is left (value 0). Return `None` if
|
||||
// `hour_of_day` is higher than 23.
|
||||
fn maybe_icecream(hour_of_day: u16) -> Option<u16> {
|
||||
fn maybe_ice_cream(hour_of_day: u16) -> Option<u16> {
|
||||
// TODO: Complete the function body.
|
||||
}
|
||||
|
||||
@@ -18,19 +18,19 @@ mod tests {
|
||||
fn raw_value() {
|
||||
// TODO: Fix this test. How do you get the value contained in the
|
||||
// Option?
|
||||
let icecreams = maybe_icecream(12);
|
||||
let ice_creams = maybe_ice_cream(12);
|
||||
|
||||
assert_eq!(icecreams, 5); // Don't change this line.
|
||||
assert_eq!(ice_creams, 5); // Don't change this line.
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_icecream() {
|
||||
assert_eq!(maybe_icecream(0), Some(5));
|
||||
assert_eq!(maybe_icecream(9), Some(5));
|
||||
assert_eq!(maybe_icecream(18), Some(5));
|
||||
assert_eq!(maybe_icecream(22), Some(0));
|
||||
assert_eq!(maybe_icecream(23), Some(0));
|
||||
assert_eq!(maybe_icecream(24), None);
|
||||
assert_eq!(maybe_icecream(25), None);
|
||||
fn check_ice_cream() {
|
||||
assert_eq!(maybe_ice_cream(0), Some(5));
|
||||
assert_eq!(maybe_ice_cream(9), Some(5));
|
||||
assert_eq!(maybe_ice_cream(18), Some(5));
|
||||
assert_eq!(maybe_ice_cream(22), Some(0));
|
||||
assert_eq!(maybe_ice_cream(23), Some(0));
|
||||
assert_eq!(maybe_ice_cream(24), None);
|
||||
assert_eq!(maybe_ice_cream(25), None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ fn main() {
|
||||
|
||||
// TODO: Fix the compiler error by adding something to this match statement.
|
||||
match optional_point {
|
||||
Some(p) => println!("Co-ordinates are {},{}", p.x, p.y),
|
||||
Some(p) => println!("Coordinates are {},{}", p.x, p.y),
|
||||
_ => panic!("No match!"),
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# Error handling
|
||||
|
||||
Most errors aren’t serious enough to require the program to stop entirely.
|
||||
Sometimes, when a function fails, it’s for a reason that you can easily interpret and respond to.
|
||||
For example, if you try to open a file and that operation fails because the file doesn’t exist, you might want to create the file instead of terminating the process.
|
||||
Most errors aren't serious enough to require the program to stop entirely.
|
||||
Sometimes, when a function fails, it's for a reason that you can easily interpret and respond to.
|
||||
For example, if you try to open a file and that operation fails because the file doesn't exist, you might want to create the file instead of terminating the process.
|
||||
|
||||
## Further information
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// of `Option<String>`.
|
||||
fn generate_nametag_text(name: String) -> Option<String> {
|
||||
if name.is_empty() {
|
||||
// Empty names aren't allowed.
|
||||
// Empty names aren't allowed
|
||||
None
|
||||
} else {
|
||||
Some(format!("Hi! My name is {name}"))
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#![allow(clippy::comparison_chain)]
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
enum CreationError {
|
||||
Negative,
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
//
|
||||
// In short, this particular use case for boxes is for when you want to own a
|
||||
// value and you care only that it is a type which implements a particular
|
||||
// trait. To do so, The `Box` is declared as of type `Box<dyn Trait>` where
|
||||
// trait. To do so, the `Box` is declared as of type `Box<dyn Trait>` where
|
||||
// `Trait` is the trait the compiler looks for on any value used in that
|
||||
// context. For this exercise, that context is the potential errors which
|
||||
// can be returned in a `Result`.
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
trait Licensed {
|
||||
// TODO: Add a default implementation for `licensing_info` so that
|
||||
// implementors like the two structs below can share that default behavior
|
||||
|
||||
@@ -8,7 +8,6 @@ use std::rc::Rc;
|
||||
#[derive(Debug)]
|
||||
struct Sun;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
enum Planet {
|
||||
Mercury(Rc<Sun>),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// This program spawns multiple threads that each run for at least 250ms, and
|
||||
// each thread returns how much time they took to complete. The program should
|
||||
// This program spawns multiple threads that each runs for at least 250ms, and
|
||||
// each thread returns how much time it took to complete. The program should
|
||||
// wait until all the spawned threads have finished and should collect their
|
||||
// return values into a vector.
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use std::{sync::mpsc, thread, time::Duration};
|
||||
|
||||
struct Queue {
|
||||
length: u32,
|
||||
first_half: Vec<u32>,
|
||||
second_half: Vec<u32>,
|
||||
}
|
||||
@@ -9,7 +8,6 @@ struct Queue {
|
||||
impl Queue {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
length: 10,
|
||||
first_half: vec![1, 2, 3, 4, 5],
|
||||
second_half: vec![6, 7, 8, 9, 10],
|
||||
}
|
||||
@@ -48,17 +46,15 @@ mod tests {
|
||||
fn threads3() {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let queue = Queue::new();
|
||||
let queue_length = queue.length;
|
||||
|
||||
send_tx(queue, tx);
|
||||
|
||||
let mut total_received: u32 = 0;
|
||||
for received in rx {
|
||||
println!("Got: {received}");
|
||||
total_received += 1;
|
||||
let mut received = Vec::with_capacity(10);
|
||||
for value in rx {
|
||||
received.push(value);
|
||||
}
|
||||
|
||||
println!("Number of received values: {total_received}");
|
||||
assert_eq!(total_received, queue_length);
|
||||
received.sort();
|
||||
assert_eq!(received, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||
}
|
||||
}
|
||||
|
||||
10
exercises/21_async/README.md
Normal file
10
exercises/21_async/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# Async
|
||||
|
||||
Rust includes built-in support for asynchronous programming. In other languages, this might be known as Promises or
|
||||
Coroutines. async programming uses async functions, which are powerful, but may require some getting used to,
|
||||
especially if you haven't used something similar in another language.
|
||||
|
||||
The [relevant book chapter][1] is essential reading. The [tokio docs][2] are also very helpful!
|
||||
|
||||
[1]: https://doc.rust-lang.org/book/ch17-00-async-await.html
|
||||
[2]: https://tokio.rs/tokio/tutorial
|
||||
44
exercises/21_async/async1.rs
Normal file
44
exercises/21_async/async1.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
// Our loyal worker works hard to create a new number.
|
||||
#[derive(Default)]
|
||||
struct Worker;
|
||||
|
||||
struct NumberContainer {
|
||||
number: i32,
|
||||
}
|
||||
|
||||
impl Worker {
|
||||
async fn work(&self) -> NumberContainer {
|
||||
// Pretend this takes a while...
|
||||
let new_number = 32;
|
||||
NumberContainer { number: new_number }
|
||||
}
|
||||
}
|
||||
|
||||
impl NumberContainer {
|
||||
async fn extract_number(&self) -> i32 {
|
||||
// And this too...
|
||||
self.number
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Fix the function signature!
|
||||
fn run_worker() -> i32 {
|
||||
// TODO: Make our worker create a new number and return it.
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Feel free to experiment here. You may need to make some adjustments
|
||||
// to this function, though.
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Don't worry about this attribute for now.
|
||||
// If you want to know what this does, read the hint!
|
||||
#[tokio::test]
|
||||
async fn test_if_it_works() {
|
||||
let number = run_worker().await;
|
||||
assert_eq!(number, 32);
|
||||
}
|
||||
}
|
||||
42
exercises/21_async/async2.rs
Normal file
42
exercises/21_async/async2.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use tokio::task::JoinSet;
|
||||
|
||||
// A MultiWorker can work with the power of 5 normal workers,
|
||||
// allowing us to create 5 new numbers at once!
|
||||
struct MultiWorker;
|
||||
|
||||
impl MultiWorker {
|
||||
async fn start_work(&self) -> JoinSet<i32> {
|
||||
let mut set = JoinSet::new();
|
||||
|
||||
for i in 30..35 {
|
||||
// TODO: `set.spawn` accepts an async function that will return the number
|
||||
// we want. Implement this function as a closure!
|
||||
set.spawn(???);
|
||||
}
|
||||
|
||||
set
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_multi_worker() -> Vec<i32> {
|
||||
let tasks = MultiWorker.start_work().await;
|
||||
|
||||
// TODO: We have a bunch of tasks, how do we run them to completion
|
||||
// to get at the i32s they create?
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Feel free to experiment here. You may need to make some adjustments
|
||||
// to this function, though.
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_if_it_works() {
|
||||
let mut numbers = run_multi_worker().await;
|
||||
numbers.sort(); // in case tasks run out-of-order
|
||||
assert_eq!(numbers, vec![30, 31, 32, 33, 34]);
|
||||
}
|
||||
}
|
||||
@@ -10,5 +10,6 @@ of exercises to Rustlings, but is all about learning to write Macros.
|
||||
|
||||
## Further information
|
||||
|
||||
- [Macros](https://doc.rust-lang.org/book/ch19-06-macros.html)
|
||||
- [The Rust Book - Macros](https://doc.rust-lang.org/book/ch20-05-macros.html)
|
||||
- [The Little Book of Rust Macros](https://veykril.github.io/tlborm/)
|
||||
- [Rust by Example - macro_rules!](https://doc.rust-lang.org/rust-by-example/macros.html)
|
||||
@@ -4,9 +4,11 @@
|
||||
#[rustfmt::skip]
|
||||
#[allow(unused_variables, unused_assignments)]
|
||||
fn main() {
|
||||
let my_option: Option<()> = None;
|
||||
let my_option: Option<&str> = None;
|
||||
// Assume that you don't know the value of `my_option`.
|
||||
// In the case of `Some`, we want to print its value.
|
||||
if my_option.is_none() {
|
||||
println!("{:?}", my_option.unwrap());
|
||||
println!("{}", my_option.unwrap());
|
||||
}
|
||||
|
||||
let my_arr = &[
|
||||
@@ -2,10 +2,11 @@
|
||||
// about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html and
|
||||
// https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
|
||||
|
||||
// Obtain the number of bytes (not characters) in the given argument.
|
||||
// Obtain the number of bytes (not characters) in the given argument
|
||||
// (`.len()` returns the number of bytes in a string).
|
||||
// TODO: Add the `AsRef` trait appropriately as a trait bound.
|
||||
fn byte_counter<T>(arg: T) -> usize {
|
||||
arg.as_ref().as_bytes().len()
|
||||
arg.as_ref().len()
|
||||
}
|
||||
|
||||
// Obtain the number of characters (not bytes) in the given argument.
|
||||
@@ -25,7 +25,7 @@ enum ParsePersonError {
|
||||
ParseInt(ParseIntError),
|
||||
}
|
||||
|
||||
// TODO: Complete this `From` implementation to be able to parse a `Person`
|
||||
// TODO: Complete this `FromStr` implementation to be able to parse a `Person`
|
||||
// out of a string in the form of "Mark,20".
|
||||
// Note that you'll need to parse the age component into a `u8` with something
|
||||
// like `"4".parse::<u8>()`.
|
||||
@@ -22,6 +22,7 @@
|
||||
| iterators | §13.2-4 |
|
||||
| smart_pointers | §15, §16.3 |
|
||||
| threads | §16.1-3 |
|
||||
| async | §17 |
|
||||
| macros | §19.5 |
|
||||
| clippy | §21.4 |
|
||||
| conversions | n/a |
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
{
|
||||
"build": {
|
||||
"path_prefix": "rustlings"
|
||||
},
|
||||
"project": {
|
||||
"homepage": "https://rustlings.cool",
|
||||
"repository": "https://github.com/rust-lang/rustlings"
|
||||
|
||||
@@ -11,3 +11,6 @@ cargo clippy -- --deny warnings
|
||||
cargo fmt --all --check
|
||||
cargo test --workspace --all-targets
|
||||
cargo run -- dev check --require-solutions
|
||||
|
||||
# MSRV
|
||||
cargo +1.85 run -- dev check --require-solutions
|
||||
|
||||
@@ -16,6 +16,9 @@ include = [
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
quote = "1.0.36"
|
||||
quote = "1.0"
|
||||
serde.workspace = true
|
||||
toml_edit.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
format_version = 1
|
||||
|
||||
welcome_message = """Is this your first time? Don't worry, Rustlings is made for beginners!
|
||||
welcome_message = """
|
||||
Is this your first time? Don't worry, Rustlings is made for beginners!
|
||||
We are going to teach you a lot of things about Rust, but before we can
|
||||
get started, here are some notes about how Rustlings operates:
|
||||
|
||||
@@ -10,15 +11,16 @@ get started, here are some notes about how Rustlings operates:
|
||||
and fix them!
|
||||
2. Make sure to have your editor open in the `rustlings/` directory. Rustlings
|
||||
will show you the path of the current exercise under the progress bar. Open
|
||||
the exercise file in your editor, fix errors and save the file. Rustlings will
|
||||
automatically detect the file change and rerun the exercise. If all errors are
|
||||
fixed, Rustlings will ask you to move on to the next exercise.
|
||||
the exercise file in your editor, fix errors and save the file. Rustlings
|
||||
will automatically detect the file change and rerun the exercise. If all
|
||||
errors are fixed, Rustlings will ask you to move on to the next exercise.
|
||||
3. If you're stuck on an exercise, enter `h` to show a hint.
|
||||
4. If an exercise doesn't make sense to you, feel free to open an issue on GitHub!
|
||||
(https://github.com/rust-lang/rustlings). We look at every issue, and sometimes,
|
||||
other learners do too so you can help each other out!"""
|
||||
4. If an exercise doesn't make sense to you, feel free to open an issue on
|
||||
GitHub! (https://github.com/rust-lang/rustlings). We look at every issue, and
|
||||
sometimes, other learners do too so you can help each other out!"""
|
||||
|
||||
final_message = """We hope you enjoyed learning about the various aspects of Rust!
|
||||
final_message = """
|
||||
We hope you enjoyed learning about the various aspects of Rust!
|
||||
If you noticed any issues, don't hesitate to report them on Github.
|
||||
You can also contribute your own exercises to help the greater community!
|
||||
|
||||
@@ -120,10 +122,10 @@ dir = "01_variables"
|
||||
test = false
|
||||
hint = """
|
||||
We know about variables and mutability, but there is another important type of
|
||||
variables available: constants.
|
||||
variable available: constants.
|
||||
|
||||
Constants are always immutable. They are declared with the keyword `const` instead
|
||||
of `let`.
|
||||
Constants are always immutable. They are declared with the keyword `const`
|
||||
instead of `let`.
|
||||
|
||||
The type of Constants must always be annotated.
|
||||
|
||||
@@ -253,7 +255,7 @@ require you to type in 100 items (but you certainly can if you want!).
|
||||
|
||||
For example, you can do:
|
||||
```
|
||||
let array = ["Are we there yet?"; 10];
|
||||
let array = ["Are we there yet?"; 100];
|
||||
```
|
||||
|
||||
Bonus: what are some other things you could have that would return `true`
|
||||
@@ -319,7 +321,8 @@ hint = """
|
||||
In the first function, we create an empty vector and want to push new elements
|
||||
to it.
|
||||
|
||||
In the second function, we map the values of the input and collect them into a vector.
|
||||
In the second function, we map the values of the input and collect them into
|
||||
a vector.
|
||||
|
||||
After you've completed both functions, decide for yourself which approach you
|
||||
like better.
|
||||
@@ -332,8 +335,8 @@ What do you think is the more commonly used pattern under Rust developers?"""
|
||||
name = "move_semantics1"
|
||||
dir = "06_move_semantics"
|
||||
hint = """
|
||||
So you've got the "cannot borrow `vec` as mutable, as it is not declared as mutable"
|
||||
error on the line where we push an element to the vector, right?
|
||||
So you've got the "cannot borrow `vec` as mutable, as it is not declared as
|
||||
mutable" error on the line where we push an element to the vector, right?
|
||||
|
||||
The fix for this is going to be adding one keyword, and the addition is NOT on
|
||||
the line where we push to the vector (where the error is).
|
||||
@@ -369,7 +372,8 @@ hint = """
|
||||
Carefully reason about the range in which each mutable reference is in
|
||||
scope. Does it help to update the value of `x` immediately after
|
||||
the mutable reference is taken?
|
||||
Read more about 'Mutable References' in the book's section 'References and Borrowing':
|
||||
Read more about 'Mutable References' in the book's section 'References and
|
||||
Borrowing':
|
||||
https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references."""
|
||||
|
||||
[[exercises]]
|
||||
@@ -508,7 +512,8 @@ name = "strings4"
|
||||
dir = "09_strings"
|
||||
test = false
|
||||
hint = """
|
||||
Replace `placeholder` with either `string` or `string_slice` in the `main` function.
|
||||
Replace `placeholder` with either `string` or `string_slice` in the `main`
|
||||
function.
|
||||
|
||||
Example:
|
||||
`placeholder("blue");`
|
||||
@@ -570,12 +575,8 @@ https://doc.rust-lang.org/book/ch08-03-hash-maps.html#only-inserting-a-value-if-
|
||||
name = "hashmaps3"
|
||||
dir = "11_hashmaps"
|
||||
hint = """
|
||||
Hint 1: Use the `entry()` and `or_insert()` (or `or_insert_with()`) methods of
|
||||
`HashMap` to insert the default value of `TeamScores` if a team doesn't
|
||||
exist in the table yet.
|
||||
|
||||
Learn more in The Book:
|
||||
https://doc.rust-lang.org/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value
|
||||
Hint 1: Use the `entry()` and `or_default()` methods of `HashMap` to insert the
|
||||
default value of `TeamScores` if a team doesn't exist in the table yet.
|
||||
|
||||
Hint 2: If there is already an entry for a given key, the value returned by
|
||||
`entry()` can be updated based on the existing value.
|
||||
@@ -763,7 +764,7 @@ Notice how the trait takes ownership of `self` and returns `Self`.
|
||||
|
||||
Although the signature of `append_bar` in the trait takes `self` as argument,
|
||||
the implementation can take `mut self` instead. This is possible because the
|
||||
the value is owned anyway."""
|
||||
value is owned anyway."""
|
||||
|
||||
[[exercises]]
|
||||
name = "traits3"
|
||||
@@ -1078,11 +1079,51 @@ original sending end.
|
||||
Related section in The Book:
|
||||
https://doc.rust-lang.org/book/ch16-02-message-passing.html"""
|
||||
|
||||
# ASYNC
|
||||
|
||||
[[exercises]]
|
||||
name = "async1"
|
||||
dir = "21_async"
|
||||
test = true
|
||||
hint = """
|
||||
Async functions are not the same as normal functions -- they have to be marked with a
|
||||
special bit of syntax, `async fn`. These functions don't immediately return or even
|
||||
execute, you have to encourage them to do so by calling another special bit of syntax
|
||||
on them.
|
||||
|
||||
Another thing - an async function can't be properly called in a normal function. Think of
|
||||
it as something contagious -- everything that it touches needs to be marked as such. Keeping
|
||||
that in mind, what adjustment do you need to make to the function signature?
|
||||
|
||||
Aside:
|
||||
`#[tokio::test]` (and `#[tokio::main]`) are "magic" attributes that automatically
|
||||
set up what we call an async runtime. The Rust compiler intentionally doesn't supply
|
||||
a default implementation of this runtime. Tokio is by far the most popular
|
||||
community-developed runtime, and this macro does a lot of the heavy lifting to let
|
||||
us use it.
|
||||
"""
|
||||
|
||||
[[exercises]]
|
||||
name = "async2"
|
||||
dir = "21_async"
|
||||
test = true
|
||||
hint = """
|
||||
Async functions can be used to run multiple things in parallel, or to more efficiently
|
||||
run things on multiple cores. Here, we use Tokio's tasks to schedule some work to run
|
||||
at the same time. We use a `JoinSet`, which is a list of tasks that lets us decide how
|
||||
to best execute them.
|
||||
|
||||
One of the ways to execute tasks is `JoinSet::join_all`, which even gives us a neat
|
||||
Vec that we can immediately return! You can also do this sequentially, with an iterator.
|
||||
See if you can also find a way to do it that doesn't use `JoinSet`! You have access to
|
||||
most of Tokio's task-based functionality here.
|
||||
"""
|
||||
|
||||
# MACROS
|
||||
|
||||
[[exercises]]
|
||||
name = "macros1"
|
||||
dir = "21_macros"
|
||||
dir = "22_macros"
|
||||
test = false
|
||||
hint = """
|
||||
When you call a macro, you need to add something special compared to a regular
|
||||
@@ -1090,7 +1131,7 @@ function call."""
|
||||
|
||||
[[exercises]]
|
||||
name = "macros2"
|
||||
dir = "21_macros"
|
||||
dir = "22_macros"
|
||||
test = false
|
||||
hint = """
|
||||
Macros don't quite play by the same rules as the rest of Rust, in terms of
|
||||
@@ -1101,7 +1142,7 @@ Unlike other things in Rust, the order of "where you define a macro" versus
|
||||
|
||||
[[exercises]]
|
||||
name = "macros3"
|
||||
dir = "21_macros"
|
||||
dir = "22_macros"
|
||||
test = false
|
||||
hint = """
|
||||
In order to use a macro outside of its module, you need to do something
|
||||
@@ -1109,7 +1150,7 @@ special to the module to lift the macro out into its parent."""
|
||||
|
||||
[[exercises]]
|
||||
name = "macros4"
|
||||
dir = "21_macros"
|
||||
dir = "22_macros"
|
||||
test = false
|
||||
hint = """
|
||||
You only need to add a single character to make this compile.
|
||||
@@ -1126,7 +1167,7 @@ https://veykril.github.io/tlborm/"""
|
||||
|
||||
[[exercises]]
|
||||
name = "clippy1"
|
||||
dir = "22_clippy"
|
||||
dir = "23_clippy"
|
||||
test = false
|
||||
strict_clippy = true
|
||||
hint = """
|
||||
@@ -1139,11 +1180,11 @@ constants, but clippy recognizes those imprecise mathematical constants as a
|
||||
source of potential error.
|
||||
|
||||
See the suggestions of the Clippy warning in the compile output and use the
|
||||
appropriate replacement constant from `std::f32::consts`..."""
|
||||
appropriate replacement constant from `std::f32::consts`."""
|
||||
|
||||
[[exercises]]
|
||||
name = "clippy2"
|
||||
dir = "22_clippy"
|
||||
dir = "23_clippy"
|
||||
test = false
|
||||
strict_clippy = true
|
||||
hint = """
|
||||
@@ -1156,7 +1197,7 @@ https://doc.rust-lang.org/std/option/#iterating-over-option"""
|
||||
|
||||
[[exercises]]
|
||||
name = "clippy3"
|
||||
dir = "22_clippy"
|
||||
dir = "23_clippy"
|
||||
test = false
|
||||
strict_clippy = true
|
||||
hint = "No hints this time!"
|
||||
@@ -1165,20 +1206,20 @@ hint = "No hints this time!"
|
||||
|
||||
[[exercises]]
|
||||
name = "using_as"
|
||||
dir = "23_conversions"
|
||||
dir = "24_conversions"
|
||||
hint = """
|
||||
Use the `as` operator to cast one of the operands in the last line of the
|
||||
`average` function into the expected return type."""
|
||||
|
||||
[[exercises]]
|
||||
name = "from_into"
|
||||
dir = "23_conversions"
|
||||
dir = "24_conversions"
|
||||
hint = """
|
||||
Follow the steps provided right before the `From` implementation."""
|
||||
|
||||
[[exercises]]
|
||||
name = "from_str"
|
||||
dir = "23_conversions"
|
||||
dir = "24_conversions"
|
||||
hint = """
|
||||
The implementation of `FromStr` should return an `Ok` with a `Person` object,
|
||||
or an `Err` with an error if the string is not valid.
|
||||
@@ -1195,15 +1236,16 @@ https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/reen
|
||||
|
||||
[[exercises]]
|
||||
name = "try_from_into"
|
||||
dir = "23_conversions"
|
||||
dir = "24_conversions"
|
||||
hint = """
|
||||
Is there an implementation of `TryFrom` in the standard library that can both do
|
||||
the required integer conversion and check the range of the input?
|
||||
|
||||
Challenge: Can you make the `TryFrom` implementations generic over many integer types?"""
|
||||
Challenge: Can you make the `TryFrom` implementations generic over many integer
|
||||
types?"""
|
||||
|
||||
[[exercises]]
|
||||
name = "as_ref_mut"
|
||||
dir = "23_conversions"
|
||||
dir = "24_conversions"
|
||||
hint = """
|
||||
Add `AsRef<str>` or `AsMut<u32>` as a trait bound to the functions."""
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
fn main() {
|
||||
let number = "T-H-R-E-E";
|
||||
println!("Spell a number: {}", number);
|
||||
println!("Spell a number: {number}");
|
||||
|
||||
// Using variable shadowing
|
||||
// https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
fn call_me(num: u32) {
|
||||
fn call_me(num: u8) {
|
||||
for i in 0..num {
|
||||
println!("Ring! Call number {}", i + 1);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
fn bigger(a: i32, b: i32) -> i32 {
|
||||
if a > b {
|
||||
a
|
||||
} else {
|
||||
b
|
||||
}
|
||||
if a > b { a } else { b }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
fn foo_if_fizz(fizzish: &str) -> &str {
|
||||
if fizzish == "fizz" {
|
||||
"foo"
|
||||
} else if fizzish == "fuzz" {
|
||||
"bar"
|
||||
fn picky_eater(food: &str) -> &str {
|
||||
if food == "strawberry" {
|
||||
"Yummy!"
|
||||
} else if food == "potato" {
|
||||
"I guess I can eat that."
|
||||
} else {
|
||||
"baz"
|
||||
"No thanks!"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,17 +17,19 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn foo_for_fizz() {
|
||||
assert_eq!(foo_if_fizz("fizz"), "foo");
|
||||
fn yummy_food() {
|
||||
assert_eq!(picky_eater("strawberry"), "Yummy!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bar_for_fuzz() {
|
||||
assert_eq!(foo_if_fizz("fuzz"), "bar");
|
||||
fn neutral_food() {
|
||||
assert_eq!(picky_eater("potato"), "I guess I can eat that.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_to_baz() {
|
||||
assert_eq!(foo_if_fizz("literally anything"), "baz");
|
||||
fn default_disliked_food() {
|
||||
assert_eq!(picky_eater("broccoli"), "No thanks!");
|
||||
assert_eq!(picky_eater("gummy bears"), "No thanks!");
|
||||
assert_eq!(picky_eater("literally anything"), "No thanks!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,6 @@ fn main() {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// TODO: Fix the compiler errors only by reordering the lines in the test.
|
||||
// Don't add, change or remove any line.
|
||||
#[test]
|
||||
fn move_semantics4() {
|
||||
let mut x = Vec::new();
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Point {
|
||||
x: u64,
|
||||
|
||||
@@ -46,8 +46,8 @@ impl State {
|
||||
match message {
|
||||
Message::Resize { width, height } => self.resize(width, height),
|
||||
Message::Move(point) => self.move_position(point),
|
||||
Message::Echo(s) => self.echo(s),
|
||||
Message::ChangeColor(r, g, b) => self.change_color(r, g, b),
|
||||
Message::Echo(string) => self.echo(string),
|
||||
Message::ChangeColor(red, green, blue) => self.change_color(red, green, blue),
|
||||
Message::Quit => self.quit(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ mod tests {
|
||||
assert_eq!(trim_me("Hello! "), "Hello!");
|
||||
assert_eq!(trim_me(" What's up!"), "What's up!");
|
||||
assert_eq!(trim_me(" Hola! "), "Hola!");
|
||||
assert_eq!(trim_me("Hi!"), "Hi!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -18,12 +18,11 @@ fn main() {
|
||||
// Here, both answers work.
|
||||
// `.into()` converts a type into an expected type.
|
||||
// If it is called where `String` is expected, it will convert `&str` to `String`.
|
||||
// But if is called where `&str` is expected, then `&str` is kept `&str` since no
|
||||
// conversion is needed.
|
||||
string("nice weather".into());
|
||||
// But if it is called where `&str` is expected, then `&str` is kept `&str` since no conversion is needed.
|
||||
// If you remove the `#[allow(…)]` line, then Clippy will tell you to remove `.into()` below since it is a useless conversion.
|
||||
#[allow(clippy::useless_conversion)]
|
||||
string_slice("nice weather".into());
|
||||
// ^^^^^^^ the compiler recommends removing the `.into()`
|
||||
// call because it is a useless conversion.
|
||||
|
||||
string(format!("Interpolation {}", "Station"));
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#[allow(dead_code)]
|
||||
mod delicious_snacks {
|
||||
// Added `pub` and used the expected alias after `as`.
|
||||
pub use self::fruits::PEAR as fruit;
|
||||
|
||||
@@ -17,7 +17,7 @@ struct TeamScores {
|
||||
|
||||
fn build_scores_table(results: &str) -> HashMap<&str, TeamScores> {
|
||||
// The name of the team is the key and its associated struct is the value.
|
||||
let mut scores = HashMap::new();
|
||||
let mut scores = HashMap::<&str, TeamScores>::new();
|
||||
|
||||
for line in results.lines() {
|
||||
let mut split_iterator = line.split(',');
|
||||
@@ -28,17 +28,13 @@ fn build_scores_table(results: &str) -> HashMap<&str, TeamScores> {
|
||||
let team_2_score: u8 = split_iterator.next().unwrap().parse().unwrap();
|
||||
|
||||
// Insert the default with zeros if a team doesn't exist yet.
|
||||
let team_1 = scores
|
||||
.entry(team_1_name)
|
||||
.or_insert_with(TeamScores::default);
|
||||
let team_1 = scores.entry(team_1_name).or_default();
|
||||
// Update the values.
|
||||
team_1.goals_scored += team_1_score;
|
||||
team_1.goals_conceded += team_2_score;
|
||||
|
||||
// Similarely for the second team.
|
||||
let team_2 = scores
|
||||
.entry(team_2_name)
|
||||
.or_insert_with(TeamScores::default);
|
||||
// Similarly for the second team.
|
||||
let team_2 = scores.entry(team_2_name).or_default();
|
||||
team_2.goals_scored += team_2_score;
|
||||
team_2.goals_conceded += team_1_score;
|
||||
}
|
||||
@@ -64,9 +60,11 @@ England,Spain,1,0";
|
||||
fn build_scores() {
|
||||
let scores = build_scores_table(RESULTS);
|
||||
|
||||
assert!(["England", "France", "Germany", "Italy", "Poland", "Spain"]
|
||||
.into_iter()
|
||||
.all(|team_name| scores.contains_key(team_name)));
|
||||
assert!(
|
||||
["England", "France", "Germany", "Italy", "Poland", "Spain"]
|
||||
.into_iter()
|
||||
.all(|team_name| scores.contains_key(team_name))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -10,7 +10,7 @@ fn main() {
|
||||
// Solution 1: Matching over the `Option` (not `&Option`) but without moving
|
||||
// out of the `Some` variant.
|
||||
match optional_point {
|
||||
Some(ref p) => println!("Co-ordinates are {},{}", p.x, p.y),
|
||||
Some(ref p) => println!("Coordinates are {},{}", p.x, p.y),
|
||||
// ^^^ added
|
||||
_ => panic!("No match!"),
|
||||
}
|
||||
@@ -18,7 +18,8 @@ fn main() {
|
||||
// Solution 2: Matching over a reference (`&Option`) by added `&` before
|
||||
// `optional_point`.
|
||||
match &optional_point {
|
||||
Some(p) => println!("Co-ordinates are {},{}", p.x, p.y),
|
||||
//^ added
|
||||
Some(p) => println!("Coordinates are {},{}", p.x, p.y),
|
||||
_ => panic!("No match!"),
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
use std::num::ParseIntError;
|
||||
|
||||
#[allow(unused_variables)]
|
||||
#[allow(unused_variables, clippy::question_mark)]
|
||||
fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||
let processing_fee = 1;
|
||||
let cost_per_item = 5;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#![allow(clippy::comparison_chain)]
|
||||
use std::cmp::Ordering;
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
enum CreationError {
|
||||
@@ -11,12 +11,10 @@ struct PositiveNonzeroInteger(u64);
|
||||
|
||||
impl PositiveNonzeroInteger {
|
||||
fn new(value: i64) -> Result<Self, CreationError> {
|
||||
if value == 0 {
|
||||
Err(CreationError::Zero)
|
||||
} else if value < 0 {
|
||||
Err(CreationError::Negative)
|
||||
} else {
|
||||
Ok(Self(value as u64))
|
||||
match value.cmp(&0) {
|
||||
Ordering::Less => Err(CreationError::Negative),
|
||||
Ordering::Equal => Err(CreationError::Zero),
|
||||
Ordering::Greater => Ok(Self(value as u64)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,21 @@ impl ParsePosNonzeroError {
|
||||
}
|
||||
}
|
||||
|
||||
// As an alternative solution, implementing the `From` trait allows for the
|
||||
// automatic conversion from a `ParseIntError` into a `ParsePosNonzeroError`
|
||||
// using the `?` operator, without the need to call `map_err`.
|
||||
//
|
||||
// ```
|
||||
// let x: i64 = s.parse()?;
|
||||
// ```
|
||||
//
|
||||
// Traits like `From` will be dealt with in later exercises.
|
||||
impl From<ParseIntError> for ParsePosNonzeroError {
|
||||
fn from(err: ParseIntError) -> Self {
|
||||
ParsePosNonzeroError::ParseInt(err)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct PositiveNonzeroInteger(u64);
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
trait Licensed {
|
||||
fn licensing_info(&self) -> String {
|
||||
"Default license".to_string()
|
||||
|
||||
@@ -5,11 +5,7 @@
|
||||
|
||||
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||
// ^^^^ ^^ ^^ ^^
|
||||
if x.len() > y.len() {
|
||||
x
|
||||
} else {
|
||||
y
|
||||
}
|
||||
if x.len() > y.len() { x } else { y }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||
if x.len() > y.len() {
|
||||
x
|
||||
} else {
|
||||
y
|
||||
}
|
||||
if x.len() > y.len() { x } else { y }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -25,6 +25,7 @@ fn factorial_fold(num: u64) -> u64 {
|
||||
// -> 1 * 2 is calculated, then the result 2 is multiplied by
|
||||
// the second element 3 so the result 6 is returned.
|
||||
// And so on…
|
||||
#[allow(clippy::unnecessary_fold)]
|
||||
(2..=num).fold(1, |acc, x| acc * x)
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ use std::rc::Rc;
|
||||
#[derive(Debug)]
|
||||
struct Sun;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
enum Planet {
|
||||
Mercury(Rc<Sun>),
|
||||
@@ -64,12 +63,10 @@ mod tests {
|
||||
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
|
||||
saturn.details();
|
||||
|
||||
// TODO
|
||||
let uranus = Planet::Uranus(Rc::clone(&sun));
|
||||
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
|
||||
uranus.details();
|
||||
|
||||
// TODO
|
||||
let neptune = Planet::Neptune(Rc::clone(&sun));
|
||||
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
|
||||
neptune.details();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// This program spawns multiple threads that each run for at least 250ms, and
|
||||
// each thread returns how much time they took to complete. The program should
|
||||
// This program spawns multiple threads that each runs for at least 250ms, and
|
||||
// each thread returns how much time it took to complete. The program should
|
||||
// wait until all the spawned threads have finished and should collect their
|
||||
// return values into a vector.
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use std::{sync::mpsc, thread, time::Duration};
|
||||
|
||||
struct Queue {
|
||||
length: u32,
|
||||
first_half: Vec<u32>,
|
||||
second_half: Vec<u32>,
|
||||
}
|
||||
@@ -9,7 +8,6 @@ struct Queue {
|
||||
impl Queue {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
length: 10,
|
||||
first_half: vec![1, 2, 3, 4, 5],
|
||||
second_half: vec![6, 7, 8, 9, 10],
|
||||
}
|
||||
@@ -50,17 +48,15 @@ mod tests {
|
||||
fn threads3() {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let queue = Queue::new();
|
||||
let queue_length = queue.length;
|
||||
|
||||
send_tx(queue, tx);
|
||||
|
||||
let mut total_received: u32 = 0;
|
||||
for received in rx {
|
||||
println!("Got: {received}");
|
||||
total_received += 1;
|
||||
let mut received = Vec::with_capacity(10);
|
||||
for value in rx {
|
||||
received.push(value);
|
||||
}
|
||||
|
||||
println!("Number of received values: {total_received}");
|
||||
assert_eq!(total_received, queue_length);
|
||||
received.sort();
|
||||
assert_eq!(received, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||
}
|
||||
}
|
||||
|
||||
45
solutions/21_async/async1.rs
Normal file
45
solutions/21_async/async1.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
// Our loyal worker works hard to create a new number.
|
||||
#[derive(Default)]
|
||||
struct Worker;
|
||||
|
||||
struct NumberContainer {
|
||||
number: i32,
|
||||
}
|
||||
|
||||
impl Worker {
|
||||
async fn work(&self) -> NumberContainer {
|
||||
// Pretend this takes a while...
|
||||
let new_number = 32;
|
||||
NumberContainer { number: new_number }
|
||||
}
|
||||
}
|
||||
|
||||
impl NumberContainer {
|
||||
async fn extract_number(&self) -> i32 {
|
||||
// And this too...
|
||||
self.number
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_worker() -> i32 {
|
||||
// TODO: Make our worker create a new number and return it.
|
||||
Worker.work().await.extract_number().await
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Feel free to experiment here. You may need to make some adjustments
|
||||
// to this function, though.
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Don't worry about this attribute for now.
|
||||
// If you want to know what this does, read the hint!
|
||||
#[tokio::test]
|
||||
// TODO: Fix the test function signature
|
||||
fn test_if_it_works() {
|
||||
let number = run_worker().await;
|
||||
assert_eq!(number, 32);
|
||||
}
|
||||
}
|
||||
43
solutions/21_async/async2.rs
Normal file
43
solutions/21_async/async2.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use tokio::task::JoinSet;
|
||||
|
||||
// A MultiWorker can work with the power of 5 normal workers,
|
||||
// allowing us to create 5 new numbers at once!
|
||||
struct MultiWorker;
|
||||
|
||||
impl MultiWorker {
|
||||
async fn start_work(&self) -> JoinSet<i32> {
|
||||
let mut set = JoinSet::new();
|
||||
|
||||
for i in 30..35 {
|
||||
// TODO: `set.spawn` accepts an async function that will return the number
|
||||
// we want. Implement this function as a closure!
|
||||
set.spawn(async move { i });
|
||||
}
|
||||
|
||||
set
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_multi_worker() -> Vec<i32> {
|
||||
let tasks = MultiWorker.start_work().await;
|
||||
|
||||
// TODO: We have a bunch of tasks, how do we run them to completion
|
||||
// to get at the i32s they create?
|
||||
tasks.join_all().await
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Feel free to experiment here. You may need to make some adjustments
|
||||
// to this function, though.
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_if_it_works() {
|
||||
let mut numbers = run_multi_worker().await;
|
||||
numbers.sort(); // in case tasks run out-of-order
|
||||
assert_eq!(numbers, vec![30, 31, 32, 33, 34]);
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,11 @@ use std::mem;
|
||||
#[rustfmt::skip]
|
||||
#[allow(unused_variables, unused_assignments)]
|
||||
fn main() {
|
||||
let my_option: Option<()> = None;
|
||||
let my_option: Option<&str> = None;
|
||||
// `unwrap` of an `Option` after checking if it is `None` will panic.
|
||||
// Use `if-let` instead.
|
||||
if let Some(value) = my_option {
|
||||
println!("{value:?}");
|
||||
println!("{value}");
|
||||
}
|
||||
|
||||
// A comma was missing.
|
||||
@@ -15,7 +15,7 @@ fn main() {
|
||||
-1, -2, -3,
|
||||
-4, -5, -6,
|
||||
];
|
||||
println!("My array! Here it is: {:?}", my_arr);
|
||||
println!("My array! Here it is: {my_arr:?}");
|
||||
|
||||
let mut my_empty_vec = vec![1, 2, 3, 4, 5];
|
||||
// `resize` mutates a vector instead of returning a new one.
|
||||
@@ -27,5 +27,5 @@ fn main() {
|
||||
let mut value_b = 66;
|
||||
// Use `mem::swap` to correctly swap two values.
|
||||
mem::swap(&mut value_a, &mut value_b);
|
||||
println!("value a: {}; value b: {}", value_a, value_b);
|
||||
println!("value a: {value_a}; value b: {value_b}");
|
||||
}
|
||||
@@ -2,9 +2,10 @@
|
||||
// about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html and
|
||||
// https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
|
||||
|
||||
// Obtain the number of bytes (not characters) in the given argument.
|
||||
// Obtain the number of bytes (not characters) in the given argument
|
||||
// (`.len()` returns the number of bytes in a string).
|
||||
fn byte_counter<T: AsRef<str>>(arg: T) -> usize {
|
||||
arg.as_ref().as_bytes().len()
|
||||
arg.as_ref().len()
|
||||
}
|
||||
|
||||
// Obtain the number of characters (not bytes) in the given argument.
|
||||
@@ -3,4 +3,4 @@
|
||||
Before you finish an exercise, its solution file will only contain an empty `main` function.
|
||||
The content of this file will be automatically replaced by the actual solution once you finish the exercise.
|
||||
|
||||
Note that these solution are often only _one possibility_ to solve an exercise.
|
||||
Note that these solutions are often only _one possibility_ to solve an exercise.
|
||||
|
||||
@@ -62,8 +62,8 @@ mod tests {
|
||||
// Import `transformer`.
|
||||
use super::my_module::transformer;
|
||||
|
||||
use super::my_module::transformer_iter;
|
||||
use super::Command;
|
||||
use super::my_module::transformer_iter;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
|
||||
470
src/app_state.rs
470
src/app_state.rs
@@ -1,32 +1,39 @@
|
||||
use anyhow::{bail, Context, Error, Result};
|
||||
use anyhow::{Context, Error, Result, bail};
|
||||
use crossterm::{QueueableCommand, cursor, terminal};
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
io::{Read, StdoutLock, Write},
|
||||
path::Path,
|
||||
collections::HashSet,
|
||||
env,
|
||||
fs::{File, OpenOptions},
|
||||
io::{Read, Seek, StdoutLock, Write},
|
||||
path::{MAIN_SEPARATOR_STR, Path},
|
||||
process::{Command, Stdio},
|
||||
sync::{
|
||||
atomic::{AtomicUsize, Ordering::Relaxed},
|
||||
mpsc,
|
||||
},
|
||||
thread,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
clear_terminal,
|
||||
cmd::CmdRunner,
|
||||
collections::hash_set_with_capacity,
|
||||
embedded::EMBEDDED_FILES,
|
||||
exercise::{Exercise, RunnableExercise},
|
||||
info_file::ExerciseInfo,
|
||||
term::{self, CheckProgressVisualizer},
|
||||
};
|
||||
|
||||
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
|
||||
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";
|
||||
const DEFAULT_CHECK_PARALLELISM: usize = 8;
|
||||
|
||||
#[must_use]
|
||||
pub enum ExercisesProgress {
|
||||
// All exercises are done.
|
||||
AllDone,
|
||||
// The current exercise failed and is still pending.
|
||||
CurrentPending,
|
||||
// A new exercise is now pending.
|
||||
NewPending,
|
||||
// The current exercise is still pending.
|
||||
CurrentPending,
|
||||
}
|
||||
|
||||
pub enum StateFileStatus {
|
||||
@@ -34,72 +41,47 @@ pub enum StateFileStatus {
|
||||
NotRead,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum CheckProgress {
|
||||
None,
|
||||
Checking,
|
||||
Done,
|
||||
Pending,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
current_exercise_ind: usize,
|
||||
exercises: Vec<Exercise>,
|
||||
// Caches the number of done exercises to avoid iterating over all exercises every time.
|
||||
n_done: u16,
|
||||
final_message: String,
|
||||
state_file: File,
|
||||
// Preallocated buffer for reading and writing the state file.
|
||||
file_buf: Vec<u8>,
|
||||
official_exercises: bool,
|
||||
cmd_runner: CmdRunner,
|
||||
// Running in VS Code.
|
||||
vs_code: bool,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
// Update the app state from the state file.
|
||||
fn update_from_file(&mut self) -> StateFileStatus {
|
||||
self.file_buf.clear();
|
||||
self.n_done = 0;
|
||||
|
||||
if File::open(STATE_FILE_NAME)
|
||||
.and_then(|mut file| file.read_to_end(&mut self.file_buf))
|
||||
.is_err()
|
||||
{
|
||||
return StateFileStatus::NotRead;
|
||||
}
|
||||
|
||||
// See `Self::write` for more information about the file format.
|
||||
let mut lines = self.file_buf.split(|c| *c == b'\n').skip(2);
|
||||
|
||||
let Some(current_exercise_name) = lines.next() else {
|
||||
return StateFileStatus::NotRead;
|
||||
};
|
||||
|
||||
if current_exercise_name.is_empty() || lines.next().is_none() {
|
||||
return StateFileStatus::NotRead;
|
||||
}
|
||||
|
||||
let mut done_exercises = hash_set_with_capacity(self.exercises.len());
|
||||
|
||||
for done_exerise_name in lines {
|
||||
if done_exerise_name.is_empty() {
|
||||
break;
|
||||
}
|
||||
done_exercises.insert(done_exerise_name);
|
||||
}
|
||||
|
||||
for (ind, exercise) in self.exercises.iter_mut().enumerate() {
|
||||
if done_exercises.contains(exercise.name.as_bytes()) {
|
||||
exercise.done = true;
|
||||
self.n_done += 1;
|
||||
}
|
||||
|
||||
if exercise.name.as_bytes() == current_exercise_name {
|
||||
self.current_exercise_ind = ind;
|
||||
}
|
||||
}
|
||||
|
||||
StateFileStatus::Read
|
||||
}
|
||||
|
||||
pub fn new(
|
||||
exercise_infos: Vec<ExerciseInfo>,
|
||||
final_message: String,
|
||||
) -> Result<(Self, StateFileStatus)> {
|
||||
let cmd_runner = CmdRunner::build()?;
|
||||
let mut state_file = OpenOptions::new()
|
||||
.create(true)
|
||||
.read(true)
|
||||
.write(true)
|
||||
.truncate(false)
|
||||
.open(STATE_FILE_NAME)
|
||||
.with_context(|| {
|
||||
format!("Failed to open or create the state file {STATE_FILE_NAME}")
|
||||
})?;
|
||||
|
||||
let exercises = exercise_infos
|
||||
let dir_canonical_path = term::canonicalize("exercises");
|
||||
let mut exercises = exercise_infos
|
||||
.into_iter()
|
||||
.map(|exercise_info| {
|
||||
// Leaking to be able to borrow in the watch mode `Table`.
|
||||
@@ -110,30 +92,97 @@ impl AppState {
|
||||
let dir = exercise_info.dir.map(|dir| &*dir.leak());
|
||||
let hint = exercise_info.hint.leak().trim_ascii();
|
||||
|
||||
let canonical_path = dir_canonical_path.as_deref().map(|dir_canonical_path| {
|
||||
let mut canonical_path;
|
||||
if let Some(dir) = dir {
|
||||
canonical_path = String::with_capacity(
|
||||
2 + dir_canonical_path.len() + dir.len() + name.len(),
|
||||
);
|
||||
canonical_path.push_str(dir_canonical_path);
|
||||
canonical_path.push_str(MAIN_SEPARATOR_STR);
|
||||
canonical_path.push_str(dir);
|
||||
} else {
|
||||
canonical_path =
|
||||
String::with_capacity(1 + dir_canonical_path.len() + name.len());
|
||||
canonical_path.push_str(dir_canonical_path);
|
||||
}
|
||||
|
||||
canonical_path.push_str(MAIN_SEPARATOR_STR);
|
||||
canonical_path.push_str(name);
|
||||
canonical_path.push_str(".rs");
|
||||
canonical_path
|
||||
});
|
||||
|
||||
Exercise {
|
||||
dir,
|
||||
name,
|
||||
path,
|
||||
canonical_path,
|
||||
test: exercise_info.test,
|
||||
strict_clippy: exercise_info.strict_clippy,
|
||||
hint,
|
||||
// Updated in `Self::update_from_file`.
|
||||
// Updated below.
|
||||
done: false,
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut slf = Self {
|
||||
current_exercise_ind: 0,
|
||||
exercises,
|
||||
n_done: 0,
|
||||
final_message,
|
||||
file_buf: Vec::with_capacity(2048),
|
||||
official_exercises: !Path::new("info.toml").exists(),
|
||||
cmd_runner,
|
||||
let mut current_exercise_ind = 0;
|
||||
let mut n_done = 0;
|
||||
let mut file_buf = Vec::with_capacity(2048);
|
||||
let state_file_status = 'block: {
|
||||
if state_file.read_to_end(&mut file_buf).is_err() {
|
||||
break 'block StateFileStatus::NotRead;
|
||||
}
|
||||
|
||||
// See `Self::write` for more information about the file format.
|
||||
let mut lines = file_buf.split(|c| *c == b'\n').skip(2);
|
||||
|
||||
let Some(current_exercise_name) = lines.next() else {
|
||||
break 'block StateFileStatus::NotRead;
|
||||
};
|
||||
|
||||
if current_exercise_name.is_empty() || lines.next().is_none() {
|
||||
break 'block StateFileStatus::NotRead;
|
||||
}
|
||||
|
||||
let mut done_exercises = HashSet::with_capacity(exercises.len());
|
||||
|
||||
for done_exercise_name in lines {
|
||||
if done_exercise_name.is_empty() {
|
||||
break;
|
||||
}
|
||||
done_exercises.insert(done_exercise_name);
|
||||
}
|
||||
|
||||
for (ind, exercise) in exercises.iter_mut().enumerate() {
|
||||
if done_exercises.contains(exercise.name.as_bytes()) {
|
||||
exercise.done = true;
|
||||
n_done += 1;
|
||||
}
|
||||
|
||||
if exercise.name.as_bytes() == current_exercise_name {
|
||||
current_exercise_ind = ind;
|
||||
}
|
||||
}
|
||||
|
||||
StateFileStatus::Read
|
||||
};
|
||||
|
||||
let state_file_status = slf.update_from_file();
|
||||
file_buf.clear();
|
||||
file_buf.extend_from_slice(STATE_FILE_HEADER);
|
||||
|
||||
let slf = Self {
|
||||
current_exercise_ind,
|
||||
exercises,
|
||||
n_done,
|
||||
final_message,
|
||||
state_file,
|
||||
file_buf,
|
||||
official_exercises: !Path::new("info.toml").exists(),
|
||||
cmd_runner,
|
||||
vs_code: env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode"),
|
||||
};
|
||||
|
||||
Ok((slf, state_file_status))
|
||||
}
|
||||
@@ -153,6 +202,11 @@ impl AppState {
|
||||
self.n_done
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn n_pending(&self) -> u16 {
|
||||
self.exercises.len() as u16 - self.n_done
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn current_exercise(&self) -> &Exercise {
|
||||
&self.exercises[self.current_exercise_ind]
|
||||
@@ -163,6 +217,11 @@ impl AppState {
|
||||
&self.cmd_runner
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn vs_code(&self) -> bool {
|
||||
self.vs_code
|
||||
}
|
||||
|
||||
// Write the state file.
|
||||
// The file's format is very simple:
|
||||
// - The first line is a comment.
|
||||
@@ -172,10 +231,8 @@ impl AppState {
|
||||
// - The fourth line is an empty line.
|
||||
// - All remaining lines are the names of done exercises.
|
||||
fn write(&mut self) -> Result<()> {
|
||||
self.file_buf.clear();
|
||||
self.file_buf.truncate(STATE_FILE_HEADER.len());
|
||||
|
||||
self.file_buf
|
||||
.extend_from_slice(b"DON'T EDIT THIS FILE!\n\n");
|
||||
self.file_buf
|
||||
.extend_from_slice(self.current_exercise().name.as_bytes());
|
||||
self.file_buf.push(b'\n');
|
||||
@@ -187,7 +244,14 @@ impl AppState {
|
||||
}
|
||||
}
|
||||
|
||||
fs::write(STATE_FILE_NAME, &self.file_buf)
|
||||
self.state_file
|
||||
.rewind()
|
||||
.with_context(|| format!("Failed to rewind the state file {STATE_FILE_NAME}"))?;
|
||||
self.state_file
|
||||
.set_len(0)
|
||||
.with_context(|| format!("Failed to truncate the state file {STATE_FILE_NAME}"))?;
|
||||
self.state_file
|
||||
.write_all(&self.file_buf)
|
||||
.with_context(|| format!("Failed to write the state file {STATE_FILE_NAME}"))?;
|
||||
|
||||
Ok(())
|
||||
@@ -219,15 +283,31 @@ impl AppState {
|
||||
self.write()
|
||||
}
|
||||
|
||||
pub fn set_pending(&mut self, exercise_ind: usize) -> Result<()> {
|
||||
// Set the status of an exercise without saving. Returns `true` if the
|
||||
// status actually changed (and thus needs saving later).
|
||||
pub fn set_status(&mut self, exercise_ind: usize, done: bool) -> Result<bool> {
|
||||
let exercise = self
|
||||
.exercises
|
||||
.get_mut(exercise_ind)
|
||||
.context(BAD_INDEX_ERR)?;
|
||||
|
||||
if exercise.done {
|
||||
exercise.done = false;
|
||||
if exercise.done == done {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
exercise.done = done;
|
||||
if done {
|
||||
self.n_done += 1;
|
||||
} else {
|
||||
self.n_done -= 1;
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
// Set the status of an exercise to "pending" and save.
|
||||
pub fn set_pending(&mut self, exercise_ind: usize) -> Result<()> {
|
||||
if self.set_status(exercise_ind, false)? {
|
||||
self.write()?;
|
||||
}
|
||||
|
||||
@@ -271,6 +351,7 @@ impl AppState {
|
||||
Ok(exercise.path)
|
||||
}
|
||||
|
||||
// Reset the exercise by index and return its name.
|
||||
pub fn reset_exercise_by_ind(&mut self, exercise_ind: usize) -> Result<&'static str> {
|
||||
if exercise_ind >= self.exercises.len() {
|
||||
bail!(BAD_INDEX_ERR);
|
||||
@@ -280,33 +361,30 @@ impl AppState {
|
||||
let exercise = &self.exercises[exercise_ind];
|
||||
self.reset(exercise_ind, exercise.path)?;
|
||||
|
||||
Ok(exercise.path)
|
||||
Ok(exercise.name)
|
||||
}
|
||||
|
||||
// Return the index of the next pending exercise or `None` if all exercises are done.
|
||||
fn next_pending_exercise_ind(&self) -> Option<usize> {
|
||||
if self.current_exercise_ind == self.exercises.len() - 1 {
|
||||
// The last exercise is done.
|
||||
// Search for exercises not done from the start.
|
||||
return self.exercises[..self.current_exercise_ind]
|
||||
.iter()
|
||||
.position(|exercise| !exercise.done);
|
||||
}
|
||||
|
||||
// The done exercise isn't the last one.
|
||||
// Search for a pending exercise after the current one and then from the start.
|
||||
match self.exercises[self.current_exercise_ind + 1..]
|
||||
.iter()
|
||||
.position(|exercise| !exercise.done)
|
||||
{
|
||||
Some(ind) => Some(self.current_exercise_ind + 1 + ind),
|
||||
None => self.exercises[..self.current_exercise_ind]
|
||||
.iter()
|
||||
.position(|exercise| !exercise.done),
|
||||
}
|
||||
let next_ind = self.current_exercise_ind + 1;
|
||||
self.exercises
|
||||
// If the exercise done isn't the last, search for pending exercises after it.
|
||||
.get(next_ind..)
|
||||
.and_then(|later_exercises| {
|
||||
later_exercises
|
||||
.iter()
|
||||
.position(|exercise| !exercise.done)
|
||||
.map(|ind| next_ind + ind)
|
||||
})
|
||||
// Search from the start.
|
||||
.or_else(|| {
|
||||
self.exercises[..self.current_exercise_ind]
|
||||
.iter()
|
||||
.position(|exercise| !exercise.done)
|
||||
})
|
||||
}
|
||||
|
||||
/// Official exercises: Dump the solution file form the binary and return its path.
|
||||
/// Official exercises: Dump the solution file from the binary and return its path.
|
||||
/// Third-party exercises: Check if a solution file exists and return its path in that case.
|
||||
pub fn current_solution_path(&self) -> Result<Option<String>> {
|
||||
if cfg!(debug_assertions) {
|
||||
@@ -320,24 +398,135 @@ impl AppState {
|
||||
.write_solution_to_disk(self.current_exercise_ind, current_exercise.name)
|
||||
.map(Some)
|
||||
} else {
|
||||
let solution_path = if let Some(dir) = current_exercise.dir {
|
||||
format!("solutions/{dir}/{}.rs", current_exercise.name)
|
||||
} else {
|
||||
format!("solutions/{}.rs", current_exercise.name)
|
||||
};
|
||||
let sol_path = current_exercise.sol_path();
|
||||
|
||||
if Path::new(&solution_path).exists() {
|
||||
return Ok(Some(solution_path));
|
||||
if Path::new(&sol_path).exists() {
|
||||
return Ok(Some(sol_path));
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn check_all_exercises_impl(&mut self, stdout: &mut StdoutLock) -> Result<Option<usize>> {
|
||||
let term_width = terminal::size()
|
||||
.context("Failed to get the terminal size")?
|
||||
.0;
|
||||
let mut progress_visualizer = CheckProgressVisualizer::build(stdout, term_width)?;
|
||||
|
||||
let next_exercise_ind = AtomicUsize::new(0);
|
||||
let mut progresses = vec![CheckProgress::None; self.exercises.len()];
|
||||
|
||||
thread::scope(|s| {
|
||||
let (exercise_progress_sender, exercise_progress_receiver) = mpsc::channel();
|
||||
let n_threads = thread::available_parallelism()
|
||||
.map_or(DEFAULT_CHECK_PARALLELISM, |count| count.get());
|
||||
|
||||
for _ in 0..n_threads {
|
||||
let exercise_progress_sender = exercise_progress_sender.clone();
|
||||
let next_exercise_ind = &next_exercise_ind;
|
||||
let slf = &self;
|
||||
thread::Builder::new()
|
||||
.spawn_scoped(s, move || {
|
||||
loop {
|
||||
let exercise_ind = next_exercise_ind.fetch_add(1, Relaxed);
|
||||
let Some(exercise) = slf.exercises.get(exercise_ind) else {
|
||||
// No more exercises.
|
||||
break;
|
||||
};
|
||||
|
||||
if exercise_progress_sender
|
||||
.send((exercise_ind, CheckProgress::Checking))
|
||||
.is_err()
|
||||
{
|
||||
break;
|
||||
};
|
||||
|
||||
let success = exercise.run_exercise(None, &slf.cmd_runner);
|
||||
let progress = match success {
|
||||
Ok(true) => CheckProgress::Done,
|
||||
Ok(false) => CheckProgress::Pending,
|
||||
Err(_) => CheckProgress::None,
|
||||
};
|
||||
|
||||
if exercise_progress_sender
|
||||
.send((exercise_ind, progress))
|
||||
.is_err()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
})
|
||||
.context("Failed to spawn a thread to check all exercises")?;
|
||||
}
|
||||
|
||||
// Drop this sender to detect when the last thread is done.
|
||||
drop(exercise_progress_sender);
|
||||
|
||||
while let Ok((exercise_ind, progress)) = exercise_progress_receiver.recv() {
|
||||
progresses[exercise_ind] = progress;
|
||||
progress_visualizer.update(&progresses)?;
|
||||
}
|
||||
|
||||
Ok::<_, Error>(())
|
||||
})?;
|
||||
|
||||
let mut first_pending_exercise_ind = None;
|
||||
for exercise_ind in 0..progresses.len() {
|
||||
match progresses[exercise_ind] {
|
||||
CheckProgress::Done => {
|
||||
self.set_status(exercise_ind, true)?;
|
||||
}
|
||||
CheckProgress::Pending => {
|
||||
self.set_status(exercise_ind, false)?;
|
||||
if first_pending_exercise_ind.is_none() {
|
||||
first_pending_exercise_ind = Some(exercise_ind);
|
||||
}
|
||||
}
|
||||
CheckProgress::None | CheckProgress::Checking => {
|
||||
// If we got an error while checking all exercises in parallel,
|
||||
// it could be because we exceeded the limit of open file descriptors.
|
||||
// Therefore, try running exercises with errors sequentially.
|
||||
progresses[exercise_ind] = CheckProgress::Checking;
|
||||
progress_visualizer.update(&progresses)?;
|
||||
|
||||
let exercise = &self.exercises[exercise_ind];
|
||||
let success = exercise.run_exercise(None, &self.cmd_runner)?;
|
||||
if success {
|
||||
progresses[exercise_ind] = CheckProgress::Done;
|
||||
} else {
|
||||
progresses[exercise_ind] = CheckProgress::Pending;
|
||||
if first_pending_exercise_ind.is_none() {
|
||||
first_pending_exercise_ind = Some(exercise_ind);
|
||||
}
|
||||
}
|
||||
self.set_status(exercise_ind, success)?;
|
||||
progress_visualizer.update(&progresses)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.write()?;
|
||||
|
||||
Ok(first_pending_exercise_ind)
|
||||
}
|
||||
|
||||
// Return the exercise index of the first pending exercise found.
|
||||
pub fn check_all_exercises(&mut self, stdout: &mut StdoutLock) -> Result<Option<usize>> {
|
||||
stdout.queue(cursor::Hide)?;
|
||||
let res = self.check_all_exercises_impl(stdout);
|
||||
stdout.queue(cursor::Show)?;
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
/// Mark the current exercise as done and move on to the next pending exercise if one exists.
|
||||
/// If all exercises are marked as done, run all of them to make sure that they are actually
|
||||
/// done. If an exercise which is marked as done fails, mark it as pending and continue on it.
|
||||
pub fn done_current_exercise(&mut self, writer: &mut StdoutLock) -> Result<ExercisesProgress> {
|
||||
pub fn done_current_exercise<const CLEAR_BEFORE_FINAL_CHECK: bool>(
|
||||
&mut self,
|
||||
stdout: &mut StdoutLock,
|
||||
) -> Result<ExercisesProgress> {
|
||||
let exercise = &mut self.exercises[self.current_exercise_ind];
|
||||
if !exercise.done {
|
||||
exercise.done = true;
|
||||
@@ -349,69 +538,39 @@ impl AppState {
|
||||
return Ok(ExercisesProgress::NewPending);
|
||||
}
|
||||
|
||||
writer.write_all(RERUNNING_ALL_EXERCISES_MSG)?;
|
||||
if CLEAR_BEFORE_FINAL_CHECK {
|
||||
clear_terminal(stdout)?;
|
||||
} else {
|
||||
stdout.write_all(b"\n")?;
|
||||
}
|
||||
|
||||
let n_exercises = self.exercises.len();
|
||||
if let Some(first_pending_exercise_ind) = self.check_all_exercises(stdout)? {
|
||||
self.set_current_exercise_ind(first_pending_exercise_ind)?;
|
||||
|
||||
let pending_exercise_ind = thread::scope(|s| {
|
||||
let handles = self
|
||||
.exercises
|
||||
.iter_mut()
|
||||
.map(|exercise| {
|
||||
s.spawn(|| {
|
||||
let success = exercise.run_exercise(None, &self.cmd_runner)?;
|
||||
exercise.done = success;
|
||||
Ok::<_, Error>(success)
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for (exercise_ind, handle) in handles.into_iter().enumerate() {
|
||||
write!(writer, "\rProgress: {exercise_ind}/{n_exercises}")?;
|
||||
writer.flush()?;
|
||||
|
||||
let success = handle.join().unwrap()?;
|
||||
if !success {
|
||||
writer.write_all(b"\n\n")?;
|
||||
return Ok(Some(exercise_ind));
|
||||
}
|
||||
}
|
||||
|
||||
Ok::<_, Error>(None)
|
||||
})?;
|
||||
|
||||
if let Some(pending_exercise_ind) = pending_exercise_ind {
|
||||
self.current_exercise_ind = pending_exercise_ind;
|
||||
self.n_done = self
|
||||
.exercises
|
||||
.iter()
|
||||
.filter(|exercise| exercise.done)
|
||||
.count() as u16;
|
||||
self.write()?;
|
||||
return Ok(ExercisesProgress::NewPending);
|
||||
}
|
||||
|
||||
// Write that the last exercise is done.
|
||||
self.write()?;
|
||||
|
||||
clear_terminal(writer)?;
|
||||
writer.write_all(FENISH_LINE.as_bytes())?;
|
||||
|
||||
let final_message = self.final_message.trim_ascii();
|
||||
if !final_message.is_empty() {
|
||||
writer.write_all(final_message.as_bytes())?;
|
||||
writer.write_all(b"\n")?;
|
||||
}
|
||||
self.render_final_message(stdout)?;
|
||||
|
||||
Ok(ExercisesProgress::AllDone)
|
||||
}
|
||||
|
||||
pub fn render_final_message(&self, stdout: &mut StdoutLock) -> Result<()> {
|
||||
clear_terminal(stdout)?;
|
||||
stdout.write_all(FENISH_LINE.as_bytes())?;
|
||||
|
||||
let final_message = self.final_message.trim_ascii();
|
||||
if !final_message.is_empty() {
|
||||
stdout.write_all(final_message.as_bytes())?;
|
||||
stdout.write_all(b"\n")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
const RERUNNING_ALL_EXERCISES_MSG: &[u8] = b"
|
||||
All exercises seem to be done.
|
||||
Recompiling and running all exercises to make sure that all of them are actually done.
|
||||
";
|
||||
|
||||
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";
|
||||
const STATE_FILE_HEADER: &[u8] = b"DON'T EDIT THIS FILE!\n\n";
|
||||
const FENISH_LINE: &str = "+----------------------------------------------------+
|
||||
| You made it to the Fe-nish line! |
|
||||
+-------------------------- ------------------------+
|
||||
@@ -443,6 +602,7 @@ mod tests {
|
||||
dir: None,
|
||||
name: "0",
|
||||
path: "exercises/0.rs",
|
||||
canonical_path: None,
|
||||
test: false,
|
||||
strict_clippy: false,
|
||||
hint: "",
|
||||
@@ -457,9 +617,11 @@ mod tests {
|
||||
exercises: vec![dummy_exercise(), dummy_exercise(), dummy_exercise()],
|
||||
n_done: 0,
|
||||
final_message: String::new(),
|
||||
state_file: tempfile::tempfile().unwrap(),
|
||||
file_buf: Vec::new(),
|
||||
official_exercises: true,
|
||||
cmd_runner: CmdRunner::build().unwrap(),
|
||||
vs_code: false,
|
||||
};
|
||||
|
||||
let mut assert = |done: [bool; 3], expected: [Option<usize>; 3]| {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use anyhow::{Context, Result};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::info_file::ExerciseInfo;
|
||||
use crate::{exercise::RunnableExercise, info_file::ExerciseInfo};
|
||||
|
||||
/// Initial capacity of the bins buffer.
|
||||
pub const BINS_BUFFER_CAPACITY: usize = 1 << 14;
|
||||
@@ -74,13 +74,13 @@ pub fn updated_cargo_toml(
|
||||
let (bins_start_ind, bins_end_ind) = bins_start_end_ind(current_cargo_toml)?;
|
||||
|
||||
let mut updated_cargo_toml = Vec::with_capacity(BINS_BUFFER_CAPACITY);
|
||||
updated_cargo_toml.extend_from_slice(current_cargo_toml[..bins_start_ind].as_bytes());
|
||||
updated_cargo_toml.extend_from_slice(¤t_cargo_toml.as_bytes()[..bins_start_ind]);
|
||||
append_bins(
|
||||
&mut updated_cargo_toml,
|
||||
exercise_infos,
|
||||
exercise_path_prefix,
|
||||
);
|
||||
updated_cargo_toml.extend_from_slice(current_cargo_toml[bins_end_ind..].as_bytes());
|
||||
updated_cargo_toml.extend_from_slice(¤t_cargo_toml.as_bytes()[bins_end_ind..]);
|
||||
|
||||
Ok(updated_cargo_toml)
|
||||
}
|
||||
|
||||
16
src/cmd.rs
16
src/cmd.rs
@@ -1,7 +1,7 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use anyhow::{Context, Result, bail};
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
io::Read,
|
||||
io::{Read, pipe},
|
||||
path::PathBuf,
|
||||
process::{Command, Stdio},
|
||||
};
|
||||
@@ -17,7 +17,7 @@ fn run_cmd(mut cmd: Command, description: &str, output: Option<&mut Vec<u8>>) ->
|
||||
};
|
||||
|
||||
let mut handle = if let Some(output) = output {
|
||||
let (mut reader, writer) = os_pipe::pipe().with_context(|| {
|
||||
let (mut reader, writer) = pipe().with_context(|| {
|
||||
format!("Failed to create a pipe to run the command `{description}``")
|
||||
})?;
|
||||
|
||||
@@ -74,12 +74,14 @@ impl CmdRunner {
|
||||
bail!("The command `cargo metadata …` failed. Are you in the `rustlings/` directory?");
|
||||
}
|
||||
|
||||
let target_dir = serde_json::de::from_slice::<CargoMetadata>(&metadata_output.stdout)
|
||||
let metadata: CargoMetadata = serde_json::de::from_slice(&metadata_output.stdout)
|
||||
.context(
|
||||
"Failed to read the field `target_directory` from the output of the command `cargo metadata …`",
|
||||
)?.target_directory;
|
||||
)?;
|
||||
|
||||
Ok(Self { target_dir })
|
||||
Ok(Self {
|
||||
target_dir: metadata.target_directory,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn cargo<'out>(
|
||||
@@ -123,7 +125,7 @@ pub struct CargoSubcommand<'out> {
|
||||
output: Option<&'out mut Vec<u8>>,
|
||||
}
|
||||
|
||||
impl<'out> CargoSubcommand<'out> {
|
||||
impl CargoSubcommand<'_> {
|
||||
#[inline]
|
||||
pub fn args<'arg, I>(&mut self, args: I) -> &mut Self
|
||||
where
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
use ahash::AHasher;
|
||||
use std::hash::BuildHasherDefault;
|
||||
|
||||
/// DOS attacks aren't a concern for Rustlings. Therefore, we use `ahash` with fixed seeds.
|
||||
pub type HashSet<T> = std::collections::HashSet<T, BuildHasherDefault<AHasher>>;
|
||||
|
||||
#[inline]
|
||||
pub fn hash_set_with_capacity<T>(capacity: usize) -> HashSet<T> {
|
||||
HashSet::with_capacity_and_hasher(capacity, BuildHasherDefault::<AHasher>::default())
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use anyhow::{Context, Result, bail};
|
||||
use clap::Subcommand;
|
||||
use std::path::PathBuf;
|
||||
|
||||
|
||||
328
src/dev/check.rs
328
src/dev/check.rs
@@ -1,7 +1,8 @@
|
||||
use anyhow::{anyhow, bail, Context, Error, Result};
|
||||
use anyhow::{Context, Error, Result, anyhow, bail};
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
fs::{self, read_dir, OpenOptions},
|
||||
collections::HashSet,
|
||||
fs::{self, OpenOptions, read_dir},
|
||||
io::{self, Read, Write},
|
||||
path::{Path, PathBuf},
|
||||
process::{Command, Stdio},
|
||||
@@ -9,14 +10,16 @@ use std::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
cargo_toml::{append_bins, bins_start_end_ind, BINS_BUFFER_CAPACITY},
|
||||
cmd::CmdRunner,
|
||||
collections::{hash_set_with_capacity, HashSet},
|
||||
exercise::{RunnableExercise, OUTPUT_CAPACITY},
|
||||
info_file::{ExerciseInfo, InfoFile},
|
||||
CURRENT_FORMAT_VERSION,
|
||||
cargo_toml::{BINS_BUFFER_CAPACITY, append_bins, bins_start_end_ind},
|
||||
cmd::CmdRunner,
|
||||
exercise::{OUTPUT_CAPACITY, RunnableExercise},
|
||||
info_file::{ExerciseInfo, InfoFile},
|
||||
};
|
||||
|
||||
const MAX_N_EXERCISES: usize = 999;
|
||||
const MAX_EXERCISE_NAME_LEN: usize = 32;
|
||||
|
||||
// Find a char that isn't allowed in the exercise's `name` or `dir`.
|
||||
fn forbidden_char(input: &str) -> Option<char> {
|
||||
input.chars().find(|c| !c.is_alphanumeric() && *c != '_')
|
||||
@@ -39,10 +42,14 @@ fn check_cargo_toml(
|
||||
|
||||
if old_bins != new_bins {
|
||||
if cfg!(debug_assertions) {
|
||||
bail!("The file `dev/Cargo.toml` is outdated. Please run `cargo run -- dev update` to update it. Then run `cargo run -- dev check` again");
|
||||
bail!(
|
||||
"The file `dev/Cargo.toml` is outdated. Run `cargo dev update` to update it. Then run `cargo run -- dev check` again"
|
||||
);
|
||||
}
|
||||
|
||||
bail!("The file `Cargo.toml` is outdated. Please run `rustlings dev update` to update it. Then run `rustlings dev check` again");
|
||||
bail!(
|
||||
"The file `Cargo.toml` is outdated. Run `rustlings dev update` to update it. Then run `rustlings dev check` again"
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -50,8 +57,8 @@ fn check_cargo_toml(
|
||||
|
||||
// Check the info of all exercises and return their paths in a set.
|
||||
fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
|
||||
let mut names = hash_set_with_capacity(info_file.exercises.len());
|
||||
let mut paths = hash_set_with_capacity(info_file.exercises.len());
|
||||
let mut names = HashSet::with_capacity(info_file.exercises.len());
|
||||
let mut paths = HashSet::with_capacity(info_file.exercises.len());
|
||||
|
||||
let mut file_buf = String::with_capacity(1 << 14);
|
||||
for exercise_info in &info_file.exercises {
|
||||
@@ -59,6 +66,11 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
|
||||
if name.is_empty() {
|
||||
bail!("Found an empty exercise name in `info.toml`");
|
||||
}
|
||||
if name.len() > MAX_EXERCISE_NAME_LEN {
|
||||
bail!(
|
||||
"The length of the exercise name `{name}` is bigger than the maximum {MAX_EXERCISE_NAME_LEN}"
|
||||
);
|
||||
}
|
||||
if let Some(c) = forbidden_char(name) {
|
||||
bail!("Char `{c}` in the exercise name `{name}` is not allowed");
|
||||
}
|
||||
@@ -73,7 +85,9 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
|
||||
}
|
||||
|
||||
if exercise_info.hint.trim_ascii().is_empty() {
|
||||
bail!("The exercise `{name}` has an empty hint. Please provide a hint or at least tell the user why a hint isn't needed for this exercise");
|
||||
bail!(
|
||||
"The exercise `{name}` has an empty hint. Please provide a hint or at least tell the user why a hint isn't needed for this exercise"
|
||||
);
|
||||
}
|
||||
|
||||
if !names.insert(name) {
|
||||
@@ -90,15 +104,28 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
|
||||
.with_context(|| format!("Failed to read the file {path}"))?;
|
||||
|
||||
if !file_buf.contains("fn main()") {
|
||||
bail!("The `main` function is missing in the file `{path}`.\nCreate at least an empty `main` function to avoid language server errors");
|
||||
bail!(
|
||||
"The `main` function is missing in the file `{path}`.\nCreate at least an empty `main` function to avoid language server errors"
|
||||
);
|
||||
}
|
||||
|
||||
if !file_buf.contains("// TODO") {
|
||||
bail!("Didn't find any `// TODO` comment in the file `{path}`.\nYou need to have at least one such comment to guide the user.");
|
||||
bail!(
|
||||
"Didn't find any `// TODO` comment in the file `{path}`.\nYou need to have at least one such comment to guide the user."
|
||||
);
|
||||
}
|
||||
|
||||
if !exercise_info.test && file_buf.contains("#[test]") {
|
||||
bail!("The file `{path}` contains tests annotated with `#[test]` but the exercise `{name}` has `test = false` in the `info.toml` file");
|
||||
let contains_tests = file_buf.contains("#[test]\n");
|
||||
if exercise_info.test {
|
||||
if !contains_tests {
|
||||
bail!(
|
||||
"The file `{path}` doesn't contain any tests. If you don't want to add tests to this exercise, set `test = false` for this exercise in the `info.toml` file"
|
||||
);
|
||||
}
|
||||
} else if contains_tests {
|
||||
bail!(
|
||||
"The file `{path}` contains tests annotated with `#[test]` but the exercise `{name}` has `test = false` in the `info.toml` file"
|
||||
);
|
||||
}
|
||||
|
||||
file_buf.clear();
|
||||
@@ -114,7 +141,10 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
|
||||
// Only one level of directory nesting is allowed.
|
||||
fn check_unexpected_files(dir: &str, allowed_rust_files: &HashSet<PathBuf>) -> Result<()> {
|
||||
let unexpected_file = |path: &Path| {
|
||||
anyhow!("Found the file `{}`. Only `README.md` and Rust files related to an exercise in `info.toml` are allowed in the `{dir}` directory", path.display())
|
||||
anyhow!(
|
||||
"Found the file `{}`. Only `README.md` and Rust files related to an exercise in `info.toml` are allowed in the `{dir}` directory",
|
||||
path.display()
|
||||
)
|
||||
};
|
||||
|
||||
for entry in read_dir(dir).with_context(|| format!("Failed to open the `{dir}` directory"))? {
|
||||
@@ -143,7 +173,10 @@ fn check_unexpected_files(dir: &str, allowed_rust_files: &HashSet<PathBuf>) -> R
|
||||
let path = entry.path();
|
||||
|
||||
if !entry.file_type().unwrap().is_file() {
|
||||
bail!("Found `{}` but expected only files. Only one level of exercise nesting is allowed", path.display());
|
||||
bail!(
|
||||
"Found `{}` but expected only files. Only one level of exercise nesting is allowed",
|
||||
path.display()
|
||||
);
|
||||
}
|
||||
|
||||
let file_name = path.file_name().unwrap();
|
||||
@@ -160,61 +193,80 @@ fn check_unexpected_files(dir: &str, allowed_rust_files: &HashSet<PathBuf>) -> R
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_exercises_unsolved(info_file: &InfoFile, cmd_runner: &CmdRunner) -> Result<()> {
|
||||
println!(
|
||||
"Running all exercises to check that they aren't already solved. This may take a while…\n",
|
||||
);
|
||||
thread::scope(|s| {
|
||||
let handles = info_file
|
||||
.exercises
|
||||
.iter()
|
||||
.filter_map(|exercise_info| {
|
||||
if exercise_info.skip_check_unsolved {
|
||||
return None;
|
||||
}
|
||||
fn check_exercises_unsolved(
|
||||
info_file: &'static InfoFile,
|
||||
cmd_runner: &'static CmdRunner,
|
||||
) -> Result<()> {
|
||||
let mut stdout = io::stdout().lock();
|
||||
stdout.write_all(b"Running all exercises to check that they aren't already solved...\n")?;
|
||||
|
||||
Some((
|
||||
exercise_info.name.as_str(),
|
||||
s.spawn(|| exercise_info.run_exercise(None, cmd_runner)),
|
||||
))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for (exercise_name, handle) in handles {
|
||||
let Ok(result) = handle.join() else {
|
||||
bail!("Panic while trying to run the exericse {exercise_name}");
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(true) => bail!(
|
||||
"The exercise {exercise_name} is already solved.\n{SKIP_CHECK_UNSOLVED_HINT}",
|
||||
),
|
||||
Ok(false) => (),
|
||||
Err(e) => return Err(e),
|
||||
let handles = info_file
|
||||
.exercises
|
||||
.iter()
|
||||
.filter_map(|exercise_info| {
|
||||
if exercise_info.skip_check_unsolved {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(
|
||||
thread::Builder::new()
|
||||
.spawn(|| exercise_info.run_exercise(None, cmd_runner))
|
||||
.map(|handle| (exercise_info.name.as_str(), handle)),
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.context("Failed to spawn a thread to check if an exercise is already solved")?;
|
||||
|
||||
let n_handles = handles.len();
|
||||
write!(stdout, "Progress: 0/{n_handles}")?;
|
||||
stdout.flush()?;
|
||||
let mut handle_num = 1;
|
||||
|
||||
for (exercise_name, handle) in handles {
|
||||
let Ok(result) = handle.join() else {
|
||||
bail!("Panic while trying to run the exercise {exercise_name}");
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(true) => {
|
||||
bail!("The exercise {exercise_name} is already solved.\n{SKIP_CHECK_UNSOLVED_HINT}",)
|
||||
}
|
||||
Ok(false) => (),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
write!(stdout, "\rProgress: {handle_num}/{n_handles}")?;
|
||||
stdout.flush()?;
|
||||
handle_num += 1;
|
||||
}
|
||||
stdout.write_all(b"\n")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_exercises(info_file: &InfoFile, cmd_runner: &CmdRunner) -> Result<()> {
|
||||
fn check_exercises(info_file: &'static InfoFile, cmd_runner: &'static CmdRunner) -> Result<()> {
|
||||
match info_file.format_version.cmp(&CURRENT_FORMAT_VERSION) {
|
||||
Ordering::Less => bail!("`format_version` < {CURRENT_FORMAT_VERSION} (supported version)\nPlease migrate to the latest format version"),
|
||||
Ordering::Greater => bail!("`format_version` > {CURRENT_FORMAT_VERSION} (supported version)\nTry updating the Rustlings program"),
|
||||
Ordering::Less => bail!(
|
||||
"`format_version` < {CURRENT_FORMAT_VERSION} (supported version)\nPlease migrate to the latest format version"
|
||||
),
|
||||
Ordering::Greater => bail!(
|
||||
"`format_version` > {CURRENT_FORMAT_VERSION} (supported version)\nTry updating the Rustlings program"
|
||||
),
|
||||
Ordering::Equal => (),
|
||||
}
|
||||
|
||||
let info_file_paths = check_info_file_exercises(info_file)?;
|
||||
let handle = thread::spawn(move || check_unexpected_files("exercises", &info_file_paths));
|
||||
let handle = thread::Builder::new()
|
||||
.spawn(move || check_exercises_unsolved(info_file, cmd_runner))
|
||||
.context("Failed to spawn a thread to check if any exercise is already solved")?;
|
||||
|
||||
let info_file_paths = check_info_file_exercises(info_file)?;
|
||||
check_unexpected_files("exercises", &info_file_paths)?;
|
||||
|
||||
check_exercises_unsolved(info_file, cmd_runner)?;
|
||||
handle.join().unwrap()
|
||||
}
|
||||
|
||||
enum SolutionCheck {
|
||||
Success { sol_path: String },
|
||||
MissingRequired,
|
||||
MissingOptional,
|
||||
RunFailure { output: Vec<u8> },
|
||||
Err(Error),
|
||||
@@ -222,103 +274,123 @@ enum SolutionCheck {
|
||||
|
||||
fn check_solutions(
|
||||
require_solutions: bool,
|
||||
info_file: &InfoFile,
|
||||
cmd_runner: &CmdRunner,
|
||||
info_file: &'static InfoFile,
|
||||
cmd_runner: &'static CmdRunner,
|
||||
) -> Result<()> {
|
||||
println!("Running all solutions. This may take a while…\n");
|
||||
thread::scope(|s| {
|
||||
let handles = info_file
|
||||
.exercises
|
||||
.iter()
|
||||
.map(|exercise_info| {
|
||||
s.spawn(|| {
|
||||
let sol_path = exercise_info.sol_path();
|
||||
if !Path::new(&sol_path).exists() {
|
||||
if require_solutions {
|
||||
return SolutionCheck::MissingRequired;
|
||||
}
|
||||
let mut stdout = io::stdout().lock();
|
||||
stdout.write_all(b"Running all solutions...\n")?;
|
||||
|
||||
return SolutionCheck::MissingOptional;
|
||||
let handles = info_file
|
||||
.exercises
|
||||
.iter()
|
||||
.map(|exercise_info| {
|
||||
thread::Builder::new().spawn(move || {
|
||||
let sol_path = exercise_info.sol_path();
|
||||
if !Path::new(&sol_path).exists() {
|
||||
if require_solutions {
|
||||
return SolutionCheck::Err(anyhow!(
|
||||
"The solution of the exercise {} is missing",
|
||||
exercise_info.name,
|
||||
));
|
||||
}
|
||||
|
||||
let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
|
||||
match exercise_info.run_solution(Some(&mut output), cmd_runner) {
|
||||
Ok(true) => SolutionCheck::Success { sol_path },
|
||||
Ok(false) => SolutionCheck::RunFailure { output },
|
||||
Err(e) => SolutionCheck::Err(e),
|
||||
}
|
||||
})
|
||||
return SolutionCheck::MissingOptional;
|
||||
}
|
||||
|
||||
let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
|
||||
match exercise_info.run_solution(Some(&mut output), cmd_runner) {
|
||||
Ok(true) => SolutionCheck::Success { sol_path },
|
||||
Ok(false) => SolutionCheck::RunFailure { output },
|
||||
Err(e) => SolutionCheck::Err(e),
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.context("Failed to spawn a thread to check a solution")?;
|
||||
|
||||
let mut sol_paths = hash_set_with_capacity(info_file.exercises.len());
|
||||
let mut fmt_cmd = Command::new("rustfmt");
|
||||
fmt_cmd
|
||||
.arg("--check")
|
||||
.arg("--edition")
|
||||
.arg("2021")
|
||||
.arg("--color")
|
||||
.arg("always")
|
||||
.stdin(Stdio::null());
|
||||
let mut sol_paths = HashSet::with_capacity(info_file.exercises.len());
|
||||
let mut fmt_cmd = Command::new("rustfmt");
|
||||
fmt_cmd
|
||||
.arg("--check")
|
||||
.arg("--edition")
|
||||
.arg("2024")
|
||||
.arg("--color")
|
||||
.arg("always")
|
||||
.stdin(Stdio::null());
|
||||
|
||||
for (exercise_info, handle) in info_file.exercises.iter().zip(handles) {
|
||||
let Ok(check_result) = handle.join() else {
|
||||
let n_handles = handles.len();
|
||||
write!(stdout, "Progress: 0/{n_handles}")?;
|
||||
stdout.flush()?;
|
||||
let mut handle_num = 1;
|
||||
|
||||
for (exercise_info, handle) in info_file.exercises.iter().zip(handles) {
|
||||
let Ok(check_result) = handle.join() else {
|
||||
bail!(
|
||||
"Panic while trying to run the solution of the exercise {}",
|
||||
exercise_info.name,
|
||||
);
|
||||
};
|
||||
|
||||
match check_result {
|
||||
SolutionCheck::Success { sol_path } => {
|
||||
fmt_cmd.arg(&sol_path);
|
||||
sol_paths.insert(PathBuf::from(sol_path));
|
||||
}
|
||||
SolutionCheck::MissingOptional => (),
|
||||
SolutionCheck::RunFailure { output } => {
|
||||
stdout.write_all(b"\n\n")?;
|
||||
stdout.write_all(&output)?;
|
||||
bail!(
|
||||
"Panic while trying to run the solution of the exericse {}",
|
||||
"Running the solution of the exercise {} failed with the error above",
|
||||
exercise_info.name,
|
||||
);
|
||||
};
|
||||
|
||||
match check_result {
|
||||
SolutionCheck::Success { sol_path } => {
|
||||
fmt_cmd.arg(&sol_path);
|
||||
sol_paths.insert(PathBuf::from(sol_path));
|
||||
}
|
||||
SolutionCheck::MissingRequired => {
|
||||
bail!(
|
||||
"The solution of the exercise {} is missing",
|
||||
exercise_info.name,
|
||||
);
|
||||
}
|
||||
SolutionCheck::MissingOptional => (),
|
||||
SolutionCheck::RunFailure { output } => {
|
||||
io::stderr().lock().write_all(&output)?;
|
||||
bail!(
|
||||
"Running the solution of the exercise {} failed with the error above",
|
||||
exercise_info.name,
|
||||
);
|
||||
}
|
||||
SolutionCheck::Err(e) => return Err(e),
|
||||
}
|
||||
SolutionCheck::Err(e) => return Err(e),
|
||||
}
|
||||
|
||||
let handle = s.spawn(move || check_unexpected_files("solutions", &sol_paths));
|
||||
write!(stdout, "\rProgress: {handle_num}/{n_handles}")?;
|
||||
stdout.flush()?;
|
||||
handle_num += 1;
|
||||
}
|
||||
stdout.write_all(b"\n")?;
|
||||
|
||||
if !fmt_cmd
|
||||
.status()
|
||||
.context("Failed to run `rustfmt` on all solution files")?
|
||||
.success()
|
||||
{
|
||||
bail!("Some solutions aren't formatted. Run `rustfmt` on them");
|
||||
}
|
||||
let handle = thread::Builder::new()
|
||||
.spawn(move || check_unexpected_files("solutions", &sol_paths))
|
||||
.context(
|
||||
"Failed to spawn a thread to check for unexpected files in the solutions directory",
|
||||
)?;
|
||||
|
||||
handle.join().unwrap()
|
||||
})
|
||||
if !fmt_cmd
|
||||
.status()
|
||||
.context("Failed to run `rustfmt` on all solution files")?
|
||||
.success()
|
||||
{
|
||||
bail!("Some solutions aren't formatted. Run `rustfmt` on them");
|
||||
}
|
||||
|
||||
handle.join().unwrap()
|
||||
}
|
||||
|
||||
pub fn check(require_solutions: bool) -> Result<()> {
|
||||
let info_file = InfoFile::parse()?;
|
||||
|
||||
if info_file.exercises.len() > MAX_N_EXERCISES {
|
||||
bail!("The maximum number of exercises is {MAX_N_EXERCISES}");
|
||||
}
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
// A hack to make `cargo run -- dev check` work when developing Rustlings.
|
||||
// A hack to make `cargo dev check` work when developing Rustlings.
|
||||
check_cargo_toml(&info_file.exercises, "dev/Cargo.toml", b"../")?;
|
||||
} else {
|
||||
check_cargo_toml(&info_file.exercises, "Cargo.toml", b"")?;
|
||||
}
|
||||
|
||||
let cmd_runner = CmdRunner::build()?;
|
||||
check_exercises(&info_file, &cmd_runner)?;
|
||||
check_solutions(require_solutions, &info_file, &cmd_runner)?;
|
||||
// Leaking is fine since they are used until the end of the program.
|
||||
let cmd_runner = Box::leak(Box::new(CmdRunner::build()?));
|
||||
let info_file = Box::leak(Box::new(info_file));
|
||||
|
||||
check_exercises(info_file, cmd_runner)?;
|
||||
check_solutions(require_solutions, info_file, cmd_runner)?;
|
||||
|
||||
println!("Everything looks fine!");
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use anyhow::{Context, Result, bail};
|
||||
use std::{
|
||||
env::set_current_dir,
|
||||
fs::{self, create_dir},
|
||||
@@ -6,7 +6,7 @@ use std::{
|
||||
process::Command,
|
||||
};
|
||||
|
||||
use crate::CURRENT_FORMAT_VERSION;
|
||||
use crate::{CURRENT_FORMAT_VERSION, init::RUST_ANALYZER_TOML};
|
||||
|
||||
// Create a directory relative to the current directory and print its path.
|
||||
fn create_rel_dir(dir_name: &str, current_dir: &str) -> Result<()> {
|
||||
@@ -55,13 +55,17 @@ pub fn new(path: &Path, no_git: bool) -> Result<()> {
|
||||
write_rel_file(
|
||||
"info.toml",
|
||||
&dir_path_str,
|
||||
format!("{INFO_FILE_BEFORE_FORMAT_VERSION}{CURRENT_FORMAT_VERSION}{INFO_FILE_AFTER_FORMAT_VERSION}"),
|
||||
format!(
|
||||
"{INFO_FILE_BEFORE_FORMAT_VERSION}{CURRENT_FORMAT_VERSION}{INFO_FILE_AFTER_FORMAT_VERSION}"
|
||||
),
|
||||
)?;
|
||||
|
||||
write_rel_file("Cargo.toml", &dir_path_str, CARGO_TOML)?;
|
||||
|
||||
write_rel_file("README.md", &dir_path_str, README)?;
|
||||
|
||||
write_rel_file("rust-analyzer.toml", &dir_path_str, RUST_ANALYZER_TOML)?;
|
||||
|
||||
create_rel_dir(".vscode", &dir_path_str)?;
|
||||
write_rel_file(
|
||||
".vscode/extensions.json",
|
||||
@@ -128,7 +132,7 @@ bin = []
|
||||
|
||||
[package]
|
||||
name = "exercises"
|
||||
edition = "2021"
|
||||
edition = "2024"
|
||||
# Don't publish the exercises on crates.io!
|
||||
publish = false
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user