Rustlingsとは
RustlingsはRustコードに慣れるための演習が詰まったパッケージです。
内容はRust By Exampleに似てます。
前提条件
- git
githubよりインストールするため、gitが必要です。
Ubuntuの場合、以下コマンドでインストールしてください。
sudo apt install git-all導入
MacOS / Linux
以下コマンドでインストールできます。
curl -L https://raw.githubusercontent.com/rust-lang/rustlings/main/install.sh | bashWindows
PowerShell(他のターミナルだとエラーになる可能性があります。)を管理者として実行して、まずは以下コマンドで実行ポリシーにRemoteSignedを設定しましょう。これは信頼できる発行者からのデジタル署名を含むスクリプトを実行可能にするものです。
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserその後、以下を実行してRustlingsをインストールしてください。
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もし、アクセス拒否のメッセージが表示されるようなら、ウィルス対策ソフトでrustlingsがコピーされるフォルダをチェック対象外にするなどを試してみてください。
完了確認
以下メッセージが表示されたらインストール完了です。
All done! Run 'rustlings' to get started.カレントフォルダに以下フォルダが作成されているか確認してください。
./rustlingsエラーが発生する場合
rust関連ツールが古いかもしれません。以下コマンドでツールを最新化してください。
rustup updateバージョン確認
rustツールの一員にrustlingsが増えましたので以下でバージョン確認が可能です。
rustlings -Vアンインストール
アンインストール対象はフォルダとコマンドの2つです。
まず、フォルダは
rm -rf rustlingsで削除し、コマンドは
cargo uninstall rustlingsでアンインストールします。
rustlingsコマンドオプション
--helpで表示されるコマンドオプションは以下です。
Rustlings is a collection of small exercises to get you used to writing and reading Rust code
Usage: rustlings [OPTIONS] [COMMAND]
Commands:
verify Verify all exercises according to the recommended order
watch Rerun `verify` when files were edited
run Run/Test a single exercise
reset Reset a single exercise using "git stash -- <filename>"
hint Return a hint for the given exercise
list List the exercises available in Rustlings
lsp Enable rust-analyzer for exercises
help Print this message or the help of the given subcommand(s)
Options:
--nocapture Show outputs from the test exercises
-h, --help Print help
-V, --version Print version
| コマンド or オプション | 内容 |
|---|---|
| verify | 推奨順ですべての演習を実行後、終了します。 |
| watch | 各ファイルの更新都度、繰り返しverifyを実行します。 |
| run | 特定の演習を一つ実行します。 |
| reset | "git stash"コマンドを使って特定の演習をリセットします。 |
| hint | 特定の演習に対してヒントを表示します。 |
| list | rustlingsで実行可能な演習を一覧表示します。 |
| lsp | 演習に対してrust-analyzerで解析します。 |
| help | コマンド毎もしくは全体のhelpを表示します。 |
| --nocapture | 各演習からの出力を表示します。 |
| -h, --help | helpを表示します。 |
| -V, --version | バージョンを表示します。 |
演習一覧
| 演習 | The Bookに書かれている章 | 演習数 | 概要 |
|---|---|---|---|
| intro | n/a | 2 | rustlingsの流れを把握する簡単な演習 |
| variables | §3.1 | 6 | 変数と可変性 |
| functions | §3.3 | 5 | 関数 |
| if | §3.5 | 3 | 制御フロー |
| primitive_types | §3.2, §4.3 | 6 | データ型とスライス型 |
| vecs | §8.1 | 2 | コレクション(ベクター) |
| move_semantics | §4.1-2 | 6 | 所有権 |
| structs | §5.1, §5.3 | 3 | 構造体 |
| enums | §6, §18.3 | 3 | 列挙型 |
| strings | §8.2 | 4 | 文字列 |
| modules | §7 | 3 | モジュール構造 |
| hashmaps | §8.3 | 3 | ハッシュマップ |
| options | §10.1 | 3 | Option<T> |
| error_handling | §9 | 6 | エラー処理 |
| generics | §10 | 2 | ジェネリック |
| traits | §10.2 | 5 | トレイト |
| tests | §11.1 | 4 | 自動テスト |
| lifetimes | §10.3 | 3 | ライフタイム |
| iterators | §13.2-4 | 5 | イテレータ |
| threads | §16.1-3 | 3 | スレッド |
| smart_pointers | §15, §16.3 | 4 | スマートポインタ |
| macros | §19.6 | 4 | マクロ |
| clippy | §21.4 | 3 | Clippy |
| conversions | n/a | 5 | 型変換 |
| quiz | n/a | 3 | 複数の演習に対する復習テスト |
演習の実行方法
最も簡単な方法はrustlingsフォルダの直下で以下コマンドを実行しましょう。
rustlings watch推奨順で演習を実行し、エラーがあれば待機、ファイルが更新されたら都度再実行してくれます。
intro
それではintroの2問です。
intro1
出力内容は以下です。
====================
Hello and
welcome to...
_ _ _
_ __ _ _ ___| |_| (_)_ __ __ _ ___
| '__| | | / __| __| | | '_ \ / _` / __|
| | | |_| \__ \ |_| | | | | | (_| \__ \
|_| \__,_|___/\__|_|_|_| |_|\__, |___/
|___/
This exercise compiles successfully. The remaining exercises contain a compiler
or logic error. The central concept behind Rustlings is to fix these errors and
solve the exercises. Good luck!
The source for this exercise is in `exercises/intro/intro1.rs`. Have a look!
Going forward, the source of the exercises will always be in the success/failure output.
If you want to use rust-analyzer, Rust's LSP implementation, make sure your editor is set
up, and then run `rustlings lsp` before continuing.
====================
You can keep working on this exercise,
or jump into the next one by removing the `I AM NOT DONE` comment:
14 | // hint.
15 |
16 | // I AM NOT DONE
17 |
18 | fn main() {
Welcome to watch mode! You can type 'help' to get an overview of the commands you can use here.コメントにある通り、// I AM NOT DONEの行(16行目)を削除すると通りました。
intro2
次はこのように出力されます。
⚠️ Compiling of exercises/intro/intro2.rs failed! Please try again. Here's the output:
error: 1 positional argument in format string, but no arguments were given
--> exercises/intro/intro2.rs:11:21
|
11 | println!("Hello {}!");
| ^^
error: aborting due to 1 previous errorこちらはprintln!マクロのプレースホルダーに引数が指定されていないよ、ってことですね。
The Bookの流れからすると
println!("Hello world!");でも良さそうですが、プレースホルダーの使い方ってことだと
println!("Hello {}!", "world");let str = "world";
println!("Hello {}!", str);でも良いですかね。
それと// I AM NOT DONEの削除も忘れずに!(いつも忘れる。。)
最後に
演習はまだまだいっぱいありますね。少しずつでも記事にしていきます。


