mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-26 00:11:49 +02:00
Compare commits
53 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6588d9be22 | ||
|
|
d27f5a7d41 | ||
|
|
e48f634f34 | ||
|
|
23403546d2 | ||
|
|
294ef8d92c | ||
|
|
be87cc9fa6 | ||
|
|
b19f74e8cf | ||
|
|
5130a57f8f | ||
|
|
dc6376788b | ||
|
|
ffb6ecaf7e | ||
|
|
34a3c440d0 | ||
|
|
386257a06e | ||
|
|
0bdb5207b5 | ||
|
|
5615dd9e7d | ||
|
|
439a07445e | ||
|
|
fedabf5f7f | ||
|
|
fdc3eb3b4e | ||
|
|
b0e079e6bf | ||
|
|
06cc363a46 | ||
|
|
47afcdcc6b | ||
|
|
c17177bdef | ||
|
|
8972e8f598 | ||
|
|
5b94016585 | ||
|
|
75b09dd69a | ||
|
|
89efbdf02d | ||
|
|
b8ff299399 | ||
|
|
b4510fc03b | ||
|
|
769483640c | ||
|
|
aaccd6f7f3 | ||
|
|
7027320a8f | ||
|
|
b759ef6b58 | ||
|
|
a18f3b8158 | ||
|
|
144e1ef018 | ||
|
|
659a99032e | ||
|
|
c5de2d686d | ||
|
|
13832d7726 | ||
|
|
b7cce5c050 | ||
|
|
7a41bbca67 | ||
|
|
9e98b78260 | ||
|
|
604166f549 | ||
|
|
96b301c04e | ||
|
|
190bb27d78 | ||
|
|
7eb66550c9 | ||
|
|
8f99209ab2 | ||
|
|
701dd9c700 | ||
|
|
b821ffdd33 | ||
|
|
102a4b92a0 | ||
|
|
283a4691aa | ||
|
|
8dc696883e | ||
|
|
a6f50fd6f3 | ||
|
|
6fae5d6114 | ||
|
|
d2179d3e84 | ||
|
|
9be012dda0 |
@@ -1155,6 +1155,87 @@
|
||||
"contributions": [
|
||||
"content"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "rytheo",
|
||||
"name": "Ryan Lowe",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/22184325?v=4",
|
||||
"profile": "https://github.com/rytheo",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "cuishuang",
|
||||
"name": "cui fliter",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/15921519?v=4",
|
||||
"profile": "http://www.dashen.tech",
|
||||
"contributions": [
|
||||
"content"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "luskwater",
|
||||
"name": "Ron Lusk",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/42529?v=4",
|
||||
"profile": "https://github.com/luskwater",
|
||||
"contributions": [
|
||||
"content"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "liby",
|
||||
"name": "Bryan Lee",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/38807139?v=4",
|
||||
"profile": "http://liby.github.io/liby/",
|
||||
"contributions": [
|
||||
"content"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "nandajavarma",
|
||||
"name": "Nandaja Varma",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/2624550?v=4",
|
||||
"profile": "http://nandaja.space",
|
||||
"contributions": [
|
||||
"doc"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "merelymyself",
|
||||
"name": "pwygab",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/88221256?v=4",
|
||||
"profile": "https://github.com/merelymyself",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "lucasgrvarela",
|
||||
"name": "Lucas Grigolon Varela",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/37870368?v=4",
|
||||
"profile": "http://linkedin.com/in/lucasgrvarela",
|
||||
"contributions": [
|
||||
"content"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "bufo24",
|
||||
"name": "Bufo",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/32884105?v=4",
|
||||
"profile": "https://github.com/bufo24",
|
||||
"contributions": [
|
||||
"content"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "jackos",
|
||||
"name": "Jack Clayton",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/77730378?v=4",
|
||||
"profile": "http://rustnote.com",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 8,
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,6 +5,7 @@ target/
|
||||
*.pdb
|
||||
exercises/clippy/Cargo.toml
|
||||
exercises/clippy/Cargo.lock
|
||||
rust-project.json
|
||||
.idea
|
||||
.vscode
|
||||
*.iml
|
||||
|
||||
@@ -4,4 +4,4 @@ tasks:
|
||||
|
||||
vscode:
|
||||
extensions:
|
||||
- rust-lang.rust@0.7.8:CvNqMTgDdt3UXt+6BCDTVg==
|
||||
- rust-lang.rust@0.7.8
|
||||
|
||||
11
AUTHORS.md
11
AUTHORS.md
@@ -165,6 +165,17 @@ authors.
|
||||
<td align="center"><a href="https://github.com/stevenfukase"><img src="https://avatars.githubusercontent.com/u/66785624?v=4?s=100" width="100px;" alt=""/><br /><sub><b>stevenfukase</b></sub></a><br /><a href="#content-stevenfukase" title="Content">🖋</a></td>
|
||||
<td align="center"><a href="https://github.com/J-S-Kim"><img src="https://avatars.githubusercontent.com/u/17569303?v=4?s=100" width="100px;" alt=""/><br /><sub><b>J-S-Kim</b></sub></a><br /><a href="#content-J-S-Kim" title="Content">🖋</a></td>
|
||||
<td align="center"><a href="https://github.com/Fointard"><img src="https://avatars.githubusercontent.com/u/9333398?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Fointard</b></sub></a><br /><a href="#content-Fointard" title="Content">🖋</a></td>
|
||||
<td align="center"><a href="https://github.com/rytheo"><img src="https://avatars.githubusercontent.com/u/22184325?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ryan Lowe</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=rytheo" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://www.dashen.tech"><img src="https://avatars.githubusercontent.com/u/15921519?v=4?s=100" width="100px;" alt=""/><br /><sub><b>cui fliter</b></sub></a><br /><a href="#content-cuishuang" title="Content">🖋</a></td>
|
||||
<td align="center"><a href="https://github.com/luskwater"><img src="https://avatars.githubusercontent.com/u/42529?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ron Lusk</b></sub></a><br /><a href="#content-luskwater" title="Content">🖋</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="http://liby.github.io/liby/"><img src="https://avatars.githubusercontent.com/u/38807139?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Bryan Lee</b></sub></a><br /><a href="#content-liby" title="Content">🖋</a></td>
|
||||
<td align="center"><a href="http://nandaja.space"><img src="https://avatars.githubusercontent.com/u/2624550?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nandaja Varma</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=nandajavarma" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/merelymyself"><img src="https://avatars.githubusercontent.com/u/88221256?v=4?s=100" width="100px;" alt=""/><br /><sub><b>pwygab</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=merelymyself" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://linkedin.com/in/lucasgrvarela"><img src="https://avatars.githubusercontent.com/u/37870368?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Lucas Grigolon Varela</b></sub></a><br /><a href="#content-lucasgrvarela" title="Content">🖋</a></td>
|
||||
<td align="center"><a href="https://github.com/bufo24"><img src="https://avatars.githubusercontent.com/u/32884105?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Bufo</b></sub></a><br /><a href="#content-bufo24" title="Content">🖋</a></td>
|
||||
<td align="center"><a href="http://rustnote.com"><img src="https://avatars.githubusercontent.com/u/77730378?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jack Clayton</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=jackos" title="Code">💻</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
||||
23
CHANGELOG.md
23
CHANGELOG.md
@@ -1,3 +1,24 @@
|
||||
<a name="4.8.0"></a>
|
||||
## 4.8.0 (2022-07-01)
|
||||
|
||||
#### Features
|
||||
|
||||
- Added a progress indicator for `rustlings watch`.
|
||||
- The installation script now checks for Rustup being installed.
|
||||
- Added a `rustlings lsp` command to enable `rust-analyzer`.
|
||||
|
||||
#### Bug Fixes
|
||||
|
||||
- **move_semantics5**: Replaced "in vogue" with "in scope" in hint.
|
||||
- **if2**: Fixed a typo in the hint.
|
||||
- **variables1**: Fixed an incorrect line reference in the hint.
|
||||
- Fixed an out of bounds check in the installation Bash script.
|
||||
|
||||
#### Housekeeping
|
||||
|
||||
- Replaced the git.io URL with the fully qualified URL because of git.io's sunsetting.
|
||||
- Removed the deprecated Rust GitPod extension.
|
||||
|
||||
<a name="4.7.1"></a>
|
||||
## 4.7.1 (2022-04-20)
|
||||
|
||||
@@ -285,7 +306,7 @@
|
||||
#### Features
|
||||
|
||||
* add Option2 exercise (#290) ([86b5c08b](https://github.com/rust-lang/rustlings/commit/86b5c08b9bea1576127a7c5f599f5752072c087d))
|
||||
* add excercise for option (#282) ([135e5d47](https://github.com/rust-lang/rustlings/commit/135e5d47a7c395aece6f6022117fb20c82f2d3d4))
|
||||
* add exercise for option (#282) ([135e5d47](https://github.com/rust-lang/rustlings/commit/135e5d47a7c395aece6f6022117fb20c82f2d3d4))
|
||||
* add new exercises for generics (#280) ([76be5e4e](https://github.com/rust-lang/rustlings/commit/76be5e4e991160f5fd9093f03ee2ba260e8f7229))
|
||||
* **ci:** add buildkite config ([b049fa2c](https://github.com/rust-lang/rustlings/commit/b049fa2c84dba0f0c8906ac44e28fd45fba51a71))
|
||||
|
||||
|
||||
23
Cargo.lock
generated
23
Cargo.lock
generated
@@ -186,6 +186,15 @@ dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "home"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654"
|
||||
dependencies = [
|
||||
"winapi 0.3.9",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indicatif"
|
||||
version = "0.16.2"
|
||||
@@ -229,9 +238,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "0.4.8"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
|
||||
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
|
||||
|
||||
[[package]]
|
||||
name = "kernel32-sys"
|
||||
@@ -433,9 +442,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.5.4"
|
||||
version = "1.5.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
|
||||
checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
@@ -456,11 +465,13 @@ dependencies = [
|
||||
"assert_cmd",
|
||||
"console",
|
||||
"glob",
|
||||
"home",
|
||||
"indicatif",
|
||||
"notify",
|
||||
"predicates",
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"toml",
|
||||
]
|
||||
|
||||
@@ -501,9 +512,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.66"
|
||||
version = "1.0.81"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "336b10da19a12ad094b59d870ebde26a45402e5b470add4b5fd03c5048a32127"
|
||||
checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "rustlings"
|
||||
version = "4.7.1"
|
||||
version = "4.8.0"
|
||||
authors = ["mokou <mokou@fastmail.com>", "Carol (Nichols || Goulding) <carol.nichols@gmail.com>"]
|
||||
edition = "2021"
|
||||
|
||||
@@ -12,6 +12,9 @@ notify = "4.0"
|
||||
toml = "0.5"
|
||||
regex = "1.5"
|
||||
serde= { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0.81"
|
||||
home = "0.5.3"
|
||||
glob = "0.3.0"
|
||||
|
||||
[[bin]]
|
||||
name = "rustlings"
|
||||
|
||||
31
README.md
31
README.md
@@ -21,9 +21,9 @@ You will need to have Rust installed. You can get it by visiting https://rustup.
|
||||
Just run:
|
||||
|
||||
```bash
|
||||
curl -L https://git.io/install-rustlings | bash
|
||||
curl -L https://raw.githubusercontent.com/rust-lang/rustlings/main/install.sh | bash
|
||||
# Or if you want it to be installed to a different path:
|
||||
curl -L https://git.io/install-rustlings | bash -s mypath/
|
||||
curl -L https://raw.githubusercontent.com/rust-lang/rustlings/main/install.sh | bash -s mypath/
|
||||
```
|
||||
|
||||
This will install Rustlings and give you access to the `rustlings` command. Run it to get started!
|
||||
@@ -39,7 +39,7 @@ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
|
||||
Then, you can run:
|
||||
|
||||
```ps1
|
||||
Start-BitsTransfer -Source https://git.io/JTL5v -Destination $env:TMP/install_rustlings.ps1; Unblock-File $env:TMP/install_rustlings.ps1; Invoke-Expression $env:TMP/install_rustlings.ps1
|
||||
Start-BitsTransfer -Source https://raw.githubusercontent.com/rust-lang/rustlings/main/install.ps1 -Destination $env:TMP/install_rustlings.ps1; Unblock-File $env:TMP/install_rustlings.ps1; Invoke-Expression $env:TMP/install_rustlings.ps1
|
||||
```
|
||||
|
||||
To install Rustlings. Same as on MacOS/Linux, you will have access to the `rustlings` command after it.
|
||||
@@ -50,15 +50,15 @@ When you get a permission denied message then you have to exclude the directory
|
||||
|
||||
[Run on Repl.it](https://repl.it/github/rust-lang/rustlings)
|
||||
|
||||
[Open in Gitpod](https://gitpod.io/#https://github.com/rust-lang/rustlings)
|
||||
[](https://gitpod.io/#https://github.com/rust-lang/rustlings)
|
||||
|
||||
## Manually
|
||||
|
||||
Basically: Clone the repository at the latest tag, run `cargo install`.
|
||||
|
||||
```bash
|
||||
# find out the latest version at https://github.com/rust-lang/rustlings/releases/latest (on edit 4.6.0)
|
||||
git clone -b 4.6.0 --depth 1 https://github.com/rust-lang/rustlings
|
||||
# find out the latest version at https://github.com/rust-lang/rustlings/releases/latest (on edit 4.7.1)
|
||||
git clone -b 4.7.1 --depth 1 https://github.com/rust-lang/rustlings
|
||||
cd rustlings
|
||||
cargo install --force --path .
|
||||
```
|
||||
@@ -126,24 +126,7 @@ After every couple of sections, there will be a quiz that'll test your knowledge
|
||||
|
||||
## Enabling `rust-analyzer`
|
||||
|
||||
`rust-analyzer` support is provided, but it depends on your editor
|
||||
whether it's enabled by default. (RLS support is not provided)
|
||||
|
||||
To enable `rust-analyzer`, you'll need to make Cargo build the project
|
||||
with the `exercises` feature, which will automatically include the `exercises/`
|
||||
subfolder in the project. The easiest way to do this is to tell your editor to
|
||||
build the project with all features (the equivalent of `cargo build --all-features`).
|
||||
For specific editor instructions:
|
||||
|
||||
- **VSCode**: Add a `.vscode/settings.json` file with the following:
|
||||
```json
|
||||
{
|
||||
"rust-analyzer.cargo.features": ["exercises"]
|
||||
}
|
||||
```
|
||||
- **IntelliJ-based Editors**: Using the Rust plugin, everything should work
|
||||
by default.
|
||||
- _Missing your editor? Feel free to contribute more instructions!_
|
||||
Run the command `rustlings lsp` which will generate a `rust-project.json` at the root of the project, this allows [rust-analyzer](https://rust-analyzer.github.io/) to parse each exercise.
|
||||
|
||||
## Continuing On
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ name = "variables1"
|
||||
path = "exercises/variables/variables1.rs"
|
||||
mode = "compile"
|
||||
hint = """
|
||||
Hint: The declaration on line 12 is missing a keyword that is needed in Rust
|
||||
Hint: The declaration on line 8 is missing a keyword that is needed in Rust
|
||||
to create a new variable binding."""
|
||||
|
||||
[[exercises]]
|
||||
@@ -158,7 +158,7 @@ path = "exercises/if/if2.rs"
|
||||
mode = "test"
|
||||
hint = """
|
||||
For that first compiler error, it's important in Rust that each conditional
|
||||
block return the same type! To get the tests passing, you will need a couple
|
||||
block returns the same type! To get the tests passing, you will need a couple
|
||||
conditions checking different input values."""
|
||||
|
||||
# TEST 1
|
||||
@@ -231,7 +231,7 @@ path = "exercises/move_semantics/move_semantics5.rs"
|
||||
mode = "compile"
|
||||
hint = """
|
||||
Carefully reason about the range in which each mutable reference is in
|
||||
vogue. Does it help to update the value of referent (x) immediately after
|
||||
scope. Does it help to update the value of referent (x) immediately after
|
||||
the mutable reference is taken? 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.
|
||||
|
||||
28
install.sh
28
install.sh
@@ -25,12 +25,21 @@ else
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -x "$(command -v rustup)" ]
|
||||
then
|
||||
echo "SUCCESS: rustup is installed"
|
||||
else
|
||||
echo "ERROR: rustup does not seem to be installed."
|
||||
echo "Please download rustup using https://rustup.rs!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -x "$(command -v rustc)" ]
|
||||
then
|
||||
echo "SUCCESS: Rust is installed"
|
||||
else
|
||||
echo "ERROR: Rust does not seem to be installed."
|
||||
echo "Please download Rust using https://rustup.rs!"
|
||||
echo "Please download Rust using rustup!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -39,7 +48,7 @@ then
|
||||
echo "SUCCESS: Cargo is installed"
|
||||
else
|
||||
echo "ERROR: Cargo does not seem to be installed."
|
||||
echo "Please download Rust and Cargo using https://rustup.rs!"
|
||||
echo "Please download Rust and Cargo using rustup!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -75,6 +84,21 @@ function vercomp() {
|
||||
then
|
||||
max_len=$len2
|
||||
fi
|
||||
|
||||
#pad right in short arr
|
||||
if [[ len1 -gt len2 ]];
|
||||
then
|
||||
for ((i = len2; i < len1; i++));
|
||||
do
|
||||
v2[$i]=0
|
||||
done
|
||||
else
|
||||
for ((i = len1; i < len2; i++));
|
||||
do
|
||||
v1[$i]=0
|
||||
done
|
||||
fi
|
||||
|
||||
for i in `seq 0 $max_len`
|
||||
do
|
||||
# Fill empty fields with zeros in v1
|
||||
|
||||
43
src/main.rs
43
src/main.rs
@@ -1,4 +1,5 @@
|
||||
use crate::exercise::{Exercise, ExerciseList};
|
||||
use crate::project::RustAnalyzerProject;
|
||||
use crate::run::run;
|
||||
use crate::verify::verify;
|
||||
use argh::FromArgs;
|
||||
@@ -20,11 +21,12 @@ use std::time::Duration;
|
||||
mod ui;
|
||||
|
||||
mod exercise;
|
||||
mod project;
|
||||
mod run;
|
||||
mod verify;
|
||||
|
||||
// In sync with crate version
|
||||
const VERSION: &str = "4.7.1";
|
||||
const VERSION: &str = "4.8.0";
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
/// Rustlings is a collection of small exercises to get you used to writing and reading Rust code
|
||||
@@ -47,6 +49,7 @@ enum Subcommands {
|
||||
Run(RunArgs),
|
||||
Hint(HintArgs),
|
||||
List(ListArgs),
|
||||
Lsp(LspArgs),
|
||||
}
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
@@ -77,6 +80,12 @@ struct HintArgs {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
#[argh(subcommand, name = "lsp")]
|
||||
/// Enable rust-analyzer for exercises
|
||||
struct LspArgs {}
|
||||
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
#[argh(subcommand, name = "list")]
|
||||
/// Lists the exercises available in Rustlings
|
||||
@@ -203,7 +212,26 @@ fn main() {
|
||||
}
|
||||
|
||||
Subcommands::Verify(_subargs) => {
|
||||
verify(&exercises, verbose).unwrap_or_else(|_| std::process::exit(1));
|
||||
verify(&exercises, (0, exercises.len()), verbose).unwrap_or_else(|_| std::process::exit(1));
|
||||
}
|
||||
|
||||
Subcommands::Lsp(_subargs) => {
|
||||
let mut project = RustAnalyzerProject::new();
|
||||
project
|
||||
.get_sysroot_src()
|
||||
.expect("Couldn't find toolchain path, do you have `rustc` installed?");
|
||||
project
|
||||
.exercies_to_json()
|
||||
.expect("Couldn't parse rustlings exercises files");
|
||||
|
||||
if project.crates.is_empty() {
|
||||
println!("Failed find any exercises, make sure you're in the `rustlings` folder");
|
||||
} else if project.write_to_disk().is_err() {
|
||||
println!("Failed to write rust-project.json to disk for rust-analyzer");
|
||||
} else {
|
||||
println!("Successfully generated rust-project.json");
|
||||
println!("rust-analyzer will now parse exercises, restart your language server or editor")
|
||||
}
|
||||
}
|
||||
|
||||
Subcommands::Watch(_subargs) => match watch(&exercises, verbose) {
|
||||
@@ -224,6 +252,7 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn spawn_watch_shell(failed_exercise_hint: &Arc<Mutex<Option<String>>>, should_quit: Arc<AtomicBool>) {
|
||||
let failed_exercise_hint = Arc::clone(failed_exercise_hint);
|
||||
println!("Welcome to watch mode! You can type 'help' to get an overview of the commands you can use here.");
|
||||
@@ -295,7 +324,7 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<WatchStatus> {
|
||||
clear_screen();
|
||||
|
||||
let to_owned_hint = |t: &Exercise| t.hint.to_owned();
|
||||
let failed_exercise_hint = match verify(exercises.iter(), verbose) {
|
||||
let failed_exercise_hint = match verify(exercises.iter(), (0, exercises.len()), verbose) {
|
||||
Ok(_) => return Ok(WatchStatus::Finished),
|
||||
Err(exercise) => Arc::new(Mutex::new(Some(to_owned_hint(exercise)))),
|
||||
};
|
||||
@@ -308,11 +337,11 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<WatchStatus> {
|
||||
let filepath = b.as_path().canonicalize().unwrap();
|
||||
let pending_exercises = exercises
|
||||
.iter()
|
||||
.skip_while(|e| !filepath.ends_with(&e.path))
|
||||
// .filter(|e| filepath.ends_with(&e.path))
|
||||
.find(|e| filepath.ends_with(&e.path)).into_iter()
|
||||
.chain(exercises.iter().filter(|e| !e.looks_done() && !filepath.ends_with(&e.path)));
|
||||
let num_done = exercises.iter().filter(|e| e.looks_done()).count();
|
||||
clear_screen();
|
||||
match verify(pending_exercises, verbose) {
|
||||
match verify(pending_exercises, (num_done, exercises.len()), verbose) {
|
||||
Ok(_) => return Ok(WatchStatus::Finished),
|
||||
Err(exercise) => {
|
||||
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();
|
||||
@@ -367,6 +396,8 @@ started, here's a couple of notes about how Rustlings operates:
|
||||
4. If an exercise doesn't make sense to you, feel free to open an issue on GitHub!
|
||||
(https://github.com/rust-lang/rustlings/issues/new). We look at every issue,
|
||||
and sometimes, other learners do too so you can help each other out!
|
||||
5. If you want to use `rust-analyzer` with exercises, which provides features like
|
||||
autocompletion, run the command `rustlings lsp`.
|
||||
|
||||
Got all that? Great! To get started, run `rustlings watch` in order to get the first
|
||||
exercise. Make sure to have your editor open!"#;
|
||||
|
||||
90
src/project.rs
Normal file
90
src/project.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
use glob::glob;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::error::Error;
|
||||
use std::process::Command;
|
||||
|
||||
/// Contains the structure of resulting rust-project.json file
|
||||
/// and functions to build the data required to create the file
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct RustAnalyzerProject {
|
||||
sysroot_src: String,
|
||||
pub crates: Vec<Crate>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Crate {
|
||||
root_module: String,
|
||||
edition: String,
|
||||
deps: Vec<String>,
|
||||
cfg: Vec<String>,
|
||||
}
|
||||
|
||||
impl RustAnalyzerProject {
|
||||
pub fn new() -> RustAnalyzerProject {
|
||||
RustAnalyzerProject {
|
||||
sysroot_src: String::new(),
|
||||
crates: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Write rust-project.json to disk
|
||||
pub fn write_to_disk(&self) -> Result<(), std::io::Error> {
|
||||
std::fs::write(
|
||||
"./rust-project.json",
|
||||
serde_json::to_vec(&self).expect("Failed to serialize to JSON"),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// If path contains .rs extension, add a crate to `rust-project.json`
|
||||
fn path_to_json(&mut self, path: String) {
|
||||
if let Some((_, ext)) = path.split_once('.') {
|
||||
if ext == "rs" {
|
||||
self.crates.push(Crate {
|
||||
root_module: path,
|
||||
edition: "2021".to_string(),
|
||||
deps: Vec::new(),
|
||||
// This allows rust_analyzer to work inside #[test] blocks
|
||||
cfg: vec!["test".to_string()],
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the exercises folder for .rs files, any matches will create
|
||||
/// a new `crate` in rust-project.json which allows rust-analyzer to
|
||||
/// treat it like a normal binary
|
||||
pub fn exercies_to_json(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
for e in glob("./exercises/**/*")? {
|
||||
let path = e?.to_string_lossy().to_string();
|
||||
self.path_to_json(path);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Use `rustc` to determine the default toolchain
|
||||
pub fn get_sysroot_src(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
let toolchain = Command::new("rustc")
|
||||
.arg("--print")
|
||||
.arg("sysroot")
|
||||
.output()?
|
||||
.stdout;
|
||||
|
||||
let toolchain = String::from_utf8_lossy(&toolchain);
|
||||
let mut whitespace_iter = toolchain.split_whitespace();
|
||||
|
||||
let toolchain = whitespace_iter.next().unwrap_or(&toolchain);
|
||||
|
||||
println!("Determined toolchain: {}\n", &toolchain);
|
||||
|
||||
self.sysroot_src = (std::path::Path::new(&*toolchain)
|
||||
.join("lib")
|
||||
.join("rustlib")
|
||||
.join("src")
|
||||
.join("rust")
|
||||
.join("library")
|
||||
.to_string_lossy())
|
||||
.to_string();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::exercise::{CompiledExercise, Exercise, Mode, State};
|
||||
use console::style;
|
||||
use indicatif::ProgressBar;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use std::env;
|
||||
|
||||
// Verify that the provided container of Exercise objects
|
||||
@@ -9,10 +9,18 @@ use std::env;
|
||||
// If the Exercise being verified is a test, the verbose boolean
|
||||
// determines whether or not the test harness outputs are displayed.
|
||||
pub fn verify<'a>(
|
||||
start_at: impl IntoIterator<Item = &'a Exercise>,
|
||||
exercises: impl IntoIterator<Item = &'a Exercise>,
|
||||
progress: (usize, usize),
|
||||
verbose: bool,
|
||||
) -> Result<(), &'a Exercise> {
|
||||
for exercise in start_at {
|
||||
let (num_done, total) = progress;
|
||||
let bar = ProgressBar::new(total as u64);
|
||||
bar.set_style(ProgressStyle::default_bar()
|
||||
.template("Progress: [{bar:60.green/red}] {pos}/{len}")
|
||||
.progress_chars("#>-")
|
||||
);
|
||||
bar.set_position(num_done as u64);
|
||||
for exercise in exercises {
|
||||
let compile_result = match exercise.mode {
|
||||
Mode::Test => compile_and_test(exercise, RunMode::Interactive, verbose),
|
||||
Mode::Compile => compile_and_run_interactively(exercise),
|
||||
@@ -21,6 +29,7 @@ pub fn verify<'a>(
|
||||
if !compile_result.unwrap_or(false) {
|
||||
return Err(exercise);
|
||||
}
|
||||
bar.inc(1);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -45,7 +54,6 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
|
||||
let _ = compile(exercise, &progress_bar)?;
|
||||
progress_bar.finish_and_clear();
|
||||
|
||||
success!("Successfully compiled {}!", exercise);
|
||||
Ok(prompt_for_completion(exercise, None))
|
||||
}
|
||||
|
||||
@@ -71,8 +79,6 @@ fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
|
||||
}
|
||||
};
|
||||
|
||||
success!("Successfully ran {}!", exercise);
|
||||
|
||||
Ok(prompt_for_completion(exercise, Some(output.stdout)))
|
||||
}
|
||||
|
||||
@@ -92,7 +98,6 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode, verbose: bool) -> Re
|
||||
if verbose {
|
||||
println!("{}", output.stdout);
|
||||
}
|
||||
success!("Successfully tested {}", &exercise);
|
||||
if let RunMode::Interactive = run_mode {
|
||||
Ok(prompt_for_completion(exercise, None))
|
||||
} else {
|
||||
@@ -138,6 +143,12 @@ fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) ->
|
||||
State::Pending(context) => context,
|
||||
};
|
||||
|
||||
match exercise.mode {
|
||||
Mode::Compile => success!("Successfully ran {}!", exercise),
|
||||
Mode::Test => success!("Successfully tested {}!", exercise),
|
||||
Mode::Clippy => success!("Successfully compiled {}!", exercise),
|
||||
}
|
||||
|
||||
let no_emoji = env::var("NO_EMOJI").is_ok();
|
||||
|
||||
let clippy_success_msg = if no_emoji {
|
||||
|
||||
Reference in New Issue
Block a user