Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

はじめてのRustプログラム

実際にRustのプログラムを書いて動かしてみましょう。

Hello, World!

プログラミング学習の伝統として、最初に書くプログラムは「Hello, World!」を表示するものです。

プロジェクトの作成

cargo new hello_world
cd hello_world

コードを見てみよう

src/main.rs を開くと、以下のコードが書かれています:

fn main() {
    println!("Hello, world!");
}

たった3行!これがRustプログラムの最小構成です。

1行ずつ解説

fn main() {
  • fn: 「function(関数)」の略。関数を定義するキーワード
  • main: 関数の名前。mainはプログラムの開始地点
  • (): 引数リスト。今は空っぽ
  • {: 関数の中身の始まり
#![allow(unused)]
fn main() {
    println!("Hello, world!");
}
  • println!: 画面に文字を出力するマクロ(!がついているのがマクロの印)
  • "Hello, world!": 表示する文字列
  • ;: 文の終わり
#![allow(unused)]
fn main() {
}
}
  • }: 関数の中身の終わり

実行

cargo run

出力:

Hello, world!

コードを変更してみよう

表示する文字を変える

src/main.rsを以下のように変更:

fn main() {
    println!("こんにちは、Rust!");
}

保存して実行:

cargo run

出力:

こんにちは、Rust!

複数行を表示する

fn main() {
    println!("1行目");
    println!("2行目");
    println!("3行目");
}

出力:

1行目
2行目
3行目

わざとエラーを起こしてみよう

エラーメッセージを読む練習をしましょう。

エラー1: セミコロン忘れ

fn main() {
    println!("Hello, world!")  // セミコロンがない!
}
cargo run

エラー:

error: expected `;`, found `}`
 --> src/main.rs:3:1
  |
2 |     println!("Hello, world!")
  |                              - help: add `;` here
3 | }
  | ^ unexpected token

読み方:

  • expected ;, found }``:セミコロンを期待したのに}が見つかった
  • help: add ; here:ここにセミコロンを追加して

エラー2: 閉じカッコ忘れ

fn main() {
    println!("Hello, world!";  // 閉じカッコがない!
}

エラー:

error: expected `)`, found `;`
 --> src/main.rs:2:29
  |
2 |     println!("Hello, world!";
  |                             ^ expected `)`

エラー3: クォート忘れ

fn main() {
    println!(Hello, world!);  // クォートがない!
}

エラー:

error: expected `(`, found `Hello`
 --> src/main.rs:2:14
  |
2 |     println!(Hello, world!);
  |              ^^^^^ expected `(`

プログラムの構造を理解する

fn main() {           // ← プログラムの入口(エントリーポイント)
    // ここに処理を書く
    println!("処理1");
    println!("処理2");
}                     // ← プログラムの終わり

Rustプログラムは必ずmain関数から実行が始まります。

コメントの書き方

fn main() {
    // これは1行コメント
    println!("Hello!");  // 行末コメント

    /*
    これは
    複数行
    コメント
    */
}

コメントはプログラムの実行に影響しません。メモとして使います。

cargo runとcargo build

cargo run

コンパイル + 実行を一度に行います。

cargo run

cargo build

コンパイルのみ行います。実行ファイルはtarget/debug/に作られます。

cargo build
./target/debug/hello_world

cargo check

コンパイルエラーがないかチェックします。実行ファイルは作りません。

cargo check

使い分け:

  • 開発中は cargo check(速い)
  • 動作確認は cargo run
  • 配布用は cargo build --release(最適化される)

実践プロジェクト

projects/p0_hello_rust/ に移動して、以下を試してみましょう。

  1. 自分の名前を表示するプログラムを書く
  2. 好きな言葉を3行表示するプログラムを書く
  3. わざとエラーを起こして、エラーメッセージを読む

まとめ

  • fn main() { } はプログラムの入口
  • println!() で文字を表示
  • ; で文を終わる
  • cargo run でコンパイル+実行
  • エラーメッセージを読む習慣をつけよう

確認テスト

Q1. Rustプログラムの実行が始まる場所はどこ?

Q2. println!! は何を意味する?

Q3. 以下のコードの出力は?
fn main() { println!("A"); println!("B"); println!("C"); }

Q4. println!("Hello, Rust!) のエラーは何?

Q5. コンパイル+実行を一度に行うコマンドは?


Phase 0 完了!

おめでとうございます!Phase 0を完了しました。

学んだこと:

  • コンピュータの基本構造(CPU、メモリ、ストレージ)
  • プログラミングとは何か
  • コンパイルとインタプリタの違い
  • Rust開発環境の構築
  • はじめてのRustプログラム

次のPhase: Phase 1: Rust基礎