1章 はじめに#
この章では以下を実行できる状態にする:
- Rustをインストールする
- Hello Worldを実行する
- Cargoを使えるようにする
1.1. インストール#
どの環境でやってもらっても問題ないが,ここではLinux(Ubuntu)にダウンロードする方法を示す.(以降,頭の"$“プロンプトなので直接打たない)
bash
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | shインストール後:
bash
$ rustc --version1.2. Hello World!#
まずは,作業するディレクトリを作成する.
bash
$ mkdir hello_rust
$ cd hello_rust
bash
$ touch main.rsコードを書く#
rust
fn main() {
println!("Hello, world!");
}コンパイル#
bash
rustc main.rs実行#
Linux / macOS#
bash
./mainWindows#
bash
main.exe4. Cargoを使う(推奨)#
プロジェクト作成#
bash
cargo new hello_cargo
cd hello_cargo実行#
bash
cargo runビルドのみ#
bash
cargo build高速チェック(おすすめ)#
bash
cargo check5. ディレクトリ構造#
text
hello_cargo/
├── Cargo.toml
└── src/
└── main.rs6. コード編集(src/main.rs)#
rust
fn main() {
println!("Hello, Cargo!");
}7. ハンズオン課題#
課題1:文字列変更#
rust
fn main() {
println!("Hello, Rust!");
}課題2:複数行出力#
rust
fn main() {
println!("Hello");
println!("Rust");
}課題3:変数(先取り)#
rust
fn main() {
let name = "Rust";
println!("Hello, {}!", name);
}8. よくあるエラー#
エラー1:printlnに!をつけ忘れる#
rust
println("Hello"); // エラー
正しくは:
rust
println!("Hello");エラー2:実行ファイルが見つからない#
Linux / macOS:
bash
./mainWindows:
bash
main.exe9. まとめ#
- Rustはコンパイル型言語
- 基本はCargoを使う
println!はマクロ