こんにちは!株式会社ゆめみ 24卒のnamniumと申します!サーバーサイドエンジニア3ヶ月目で、先輩から色々と学ばせていただいております
ゆめみでは社内勉強会が活発です!私もRust勉強会に参加させてもらい、 rustlings に取り組んだりしていました。
そんな折、X (旧Twitter) でrustlingsに似た新しいチュートリアルサイトの存在を知りました!
rustlingsはRust至高の公式ドキュメントThe Rust Programming Language (通称TRPL) に従った内容だったのですが、100-exercisesはトレイトや非同期処理等についてTRPLより深く踏み込んだ内容になっており、やりがいがありそうです!
Rust力を盤石にするため、Qiita Engineer Fest 2024の力を借りてこの問題集に取り組んでみたいと思います!
初回は、問題に取り組むための環境構築をメインに書きます。
環境構築
筆者は今回 Ubuntu 24.10
上で取り組むことにします!インストール方法は公式サイトから確認できます。
...といっても難しい部分はまったくなく、Rust本体のインストールは、管理者権限要らずのたった1行で行うことができます!
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer
Welcome to Rust!
~~ 省略 ~~
Current installation options:
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
>
ここで1
を選択します。
1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
>1
~~ 省略 ~~
stable-x86_64-unknown-linux-gnu installed - rustc 1.78.0 (9b00956e5 2024-04-29)
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).
To configure your current shell, you need to source
the corresponding env file under $HOME/.cargo.
This is usually done by running one of the following (note the leading DOT):
. "$HOME/.cargo/env" # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish" # For fish
今のターミナルにてそのまま cargo
コマンド等を打ちたい場合、~/.cargo/env
を読み込ませます。ついでにバージョンも確認
. "$HOME/.cargo/env"
cargo --version
cargo 1.78.0 (54d8815d0 2024-03-26)
今月来るらしい 1.80
には cargo-script
等面白い機能が色々あるみたいなんですけどね...とりあえず 1.78.0
を前提として進めていきます!
これで準備完了...と行きたいところですが、Rustのコンパイルには gcc
等のC言語コンパイル環境の力が必要で、まっさらな状態のUbuntuから始めると以下のようなエラーが出るかもしれません。
$ cargo run
Compiling project v0.1.0 (/path/to/project)
error: linker `cc` not found
|
= note: No such file or directory (os error 2)
error: could not compile `project` (bin "project") due to 1 previous error
apt
で build-essential
パッケージを入れれば gcc
等もインストールされ、無事コンパイルできるようになります。
sudo apt update
sudo apt install build-essential
また reqwest クレートを使用する場合に libssl-dev
が必要となったり等、他にもRustプログラミングをする上で色々と入れておいたほうが良いパッケージはありますが、エクササイズで必要になった際に都度入れることとします。
100 Exercises の導入
100-exercises-to-learn-rust を導入していきます。gitでクローンして取ってくる方式のようです。
git clone https://github.com/mainmatter/100-exercises-to-learn-rust.git
cd 100-exercises-to-learn-rust
git checkout -b my-solutions
これで main
ブランチの内容が手元にダウンロードされます。進捗管理のため my-solutions
ブランチにて管理することをおすすめされたので、そのようにしています。 exercises
ディレクトリ以下に問題があります。
なお、 solutions
ブランチ に模範解答があるらしいのでわからなくなったらそちらを適宜見たいと思います。
Workshop Runner のインストール
rustlingsに専用の確認ツールがあったように、100 Exercisesの方にもチェック用ツールがあるようです。 cargo install
でインストールします。
cargo install --locked workshop-runner
wr --help
$ wr --help
A CLI to run test-driven Rust workshops
Usage: wr [OPTIONS] [COMMAND]
Commands:
open Open a specific exercise
help Print this message or the help of the given subcommand(s)
Options:
--no-skip ...
--verbose ...
--keep-going ...
-h, --help Print help
-V, --version Print version
進捗管理を行ってくれるコマンドらしいです。問題を解き終えたら打ちましょう。
エディタについて
デファクトスタンダードはVSCode + rust-analyzerのため、筆者もこの組み合わせで行います。rust-analyzerは導入後特に設定する必要もないので、詳細は省略します。
その他の選択肢としては有料ですが最近正式リリースされたRustRoverが良さげです。というか気になってる...
[01_intro/00_welcome] 最初の1問をやってみる
ここまでで無事環境構築が完了したので、いよいよ問題に取り組んでみたいと思います。wr
コマンドを打ちy
を選ぶと最初の問題に移ります。
$ wr
Running tests...
Eternity lies ahead of us, and behind. Your path is not yet finished. 🍂
Do you want to open the next exercise, (01) intro - (00) welcome? [y/n] y
Ahead of you lies (01) intro - (00) welcome
Open "exercises/01_intro/00_welcome" in your editor and get started!
Run `wr` again to compile the exercise and execute its tests.
exercises/01_intro/00_welcome/src/lib.rs
を開いてみます。コメントにて、以下のようなことが書かれています。
-
//
は単行コメント -
TODO
やtodo!()
、__
はエクササイズにて取り組んでほしい箇所の強調である - コードのチェックはRust標準のテスト機構をそのまま用いている(
cargo test
コマンドで確認できるやつ)。なお、wr
コマンドが代替して行ってくれる- テストは変更しないでね
では、早速最初の1問を片付けて次の問題に進みましょう!
fn greeting() -> &'static str {
// TODO: fix me 👇
"I'm ready to __!"
}
#[cfg(test)]
mod tests {
use crate::greeting;
#[test]
fn test_welcome() {
assert_eq!(greeting(), "I'm ready to learn Rust!");
}
}
コメントあり
// This is a Rust file. It is a plain text file with a `.rs` extension.
//
// Like most modern programming languages, Rust supports comments. You're looking at one right now!
// Comments are ignored by the compiler; you can leverage them to annotate code with notes and
// explanations.
// There are various ways to write comments in Rust, each with its own purpose.
// For now we'll stick to the most common one: the line comment.
// Everything from `//` to the end of the line is considered a comment.
// Exercises will include `TODO`, `todo!()` or `__` markers to draw your attention to the lines
// where you need to write code.
// You'll need to replace these markers with your own code to complete the exercise.
// Sometimes it'll be enough to write a single line of code, other times you'll have to write
// longer sections.
//
// If you get stuck for more than 10 minutes on an exercise, grab a trainer! We're here to help!
// You can also find solutions to all exercises in the `solutions` git branch.
fn greeting() -> &'static str {
// TODO: fix me 👇
"I'm ready to __!"
}
// Your solutions will be automatically verified by a set of tests.
// You can run these tests directly by invoking the `cargo test` command in your terminal,
// from the root of this exercise's directory. That's what the `wr` command does for you
// under the hood.
//
// Rust lets you write tests alongside your code.
// The `#[cfg(test)]` attribute tells the compiler to only compile the code below when
// running tests (i.e. when you run `cargo test`).
// You'll learn more about attributes and testing later in the course.
// For now, just know that you need to look for the `#[cfg(test)]` attribute to find the tests
// that will be verifying the correctness of your solutions!
//
// ⚠️ **DO NOT MODIFY THE TESTS** ⚠️
// They are there to help you validate your solutions. You should only change the code that's being
// tested, not the tests themselves.
#[cfg(test)]
mod tests {
use crate::greeting;
#[test]
fn test_welcome() {
assert_eq!(greeting(), "I'm ready to learn Rust!");
}
}
テストが通るように直せばクリアです。
解説
単に、文字列を書き換えれば良さそうです!
fn greeting() -> &'static str {
// TODO: fix me 👇
- "I'm ready to __!"
+ "I'm ready to learn Rust!"
}
一応関連する細かい解説をしておくと、assert_eq!
という2つの引数を受け取る関数みたいなやつ(マクロ)は、与えられた2引数が等しいかどうかを判定しています。
「文字列の比較」になるので、他言語話者的には警戒する場面かもしれませんが安心してください。多分後の回で紹介されると思いますが、Rustにおいて ==
での比較は eqメソッドにより行われ、文字列については直感通り「頭から文字を比較していって全て等しいかを確認する」処理になっております。ポインタが等しいかとか直感に反する比較はありません1。
問題が解けたのでまたプロジェクトルートから wr
コマンドを打って正しいことを確認します。
$ wr
Running tests...
🚀 (01) intro - (00) welcome
Eternity lies ahead of us, and behind. Your path is not yet finished. 🍂
Do you want to open the next exercise, (01) intro - (01) syntax? [y/n] y
Ahead of you lies (01) intro - (01) syntax
Open "exercises/01_intro/01_syntax" in your editor and get started!
Run `wr` again to compile the exercise and execute its tests.
大丈夫そうです!では次の問題に行ってみましょう!
次の記事: 【1】 構文・整数・変数
-
サードパーティやユーザーが作成した型についてはstd::cmp::PartialEqトレイトの実装方法次第になるため注意が必要です。 ↩