第2章: 環境構築とCargo

学習目標

  • Rustの開発環境を正しくセットアップする
  • Cargoの基本的な使い方を理解する
  • プロジェクト構造のベストプラクティスを学ぶ
  • Cratesエコシステムの活用方法を知る

---

2.1 Rustのインストール

2.1.1 Rustupによるインストール

RustupはRustの公式インストーラーであり、ツールチェーンマネージャーです。

Linux / macOS

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Windows

インストール後の確認

rustc --version    # コンパイラのバージョン
cargo --version    # Cargoのバージョン
rustup --version   # Rustupのバージョン

期待される出力例:

rustc 1.75.0 (82e1608df 2023-12-21)
cargo 1.75.0 (1d8b05cdd 2023-11-20)
rustup 1.26.0 (5af9b9484 2023-04-05)

2.1.2 Rustupの仕組み

┌─────────────────────────────────────────────────────────┐
│              Rustupツールチェーン管理                     │
├─────────────────────────────────────────────────────────┤
│                                                         │
│   Rustup(ツールチェーンマネージャー)                    │
│      │                                                  │
│      ├─► stable    (安定版)                            │
│      │    ├─ rustc  (コンパイラ)                       │
│      │    ├─ cargo  (ビルドツール)                     │
│      │    └─ rustfmt(フォーマッター)                   │
│      │                                                  │
│      ├─► beta      (ベータ版)                          │
│      │                                                  │
│      └─► nightly   (最新実験版)                        │
│                                                         │
│   クロスコンパイルターゲットの管理                         │
│      ├─ x86_64-unknown-linux-gnu                        │
│      ├─ aarch64-apple-darwin                            │
│      └─ wasm32-unknown-unknown                          │
│                                                         │
└─────────────────────────────────────────────────────────┘

チャンネルの切り替え

# デフォルトをstableに設定(推奨)
rustup default stable

# nightlyを試す(新機能を使いたい場合)
rustup default nightly

# プロジェクトごとに指定
rustup override set nightly

2.1.3 クロスコンパイルターゲットの追加

# WebAssemblyターゲット
rustup target add wasm32-unknown-unknown

# ARM64 Linux
rustup target add aarch64-unknown-linux-gnu

# Windows(macOS/Linuxから)
rustup target add x86_64-pc-windows-gnu

# インストール済みターゲットの確認
rustup target list --installed

---

2.2 Cargoの基本

2.2.1 Cargoとは何か?

CargoはRustの公式ビルドツール兼パッケージマネージャーです:

┌─────────────────────────────────────────────────────────┐
│                    Cargoの役割                           │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  ┌────────────────┐     ┌────────────────┐             │
│  │  ビルド管理     │     │  依存関係管理   │             │
│  │  ・コンパイル   │     │  ・Crates.io    │             │
│  │  ・最適化       │     │  ・バージョン   │             │
│  │  ・ターゲット   │     │  ・解決         │             │
│  └────────────────┘     └────────────────┘             │
│                                                         │
│  ┌────────────────┐     ┌────────────────┐             │
│  │  テスト実行     │     │  プロジェクト   │             │
│  │  ・ユニット     │     │  ・雛形生成     │             │
│  │  ・統合         │     │  ・ワークスペース│            │
│  │  ・ドキュメント │     │  ・公開         │             │
│  └────────────────┘     └────────────────┘             │
│                                                         │
└─────────────────────────────────────────────────────────┘

他の言語との比較

言語 ビルドツール パッケージマネージャー
Rust Cargo Cargo
Go go build go get
Node.js npm scripts npm/yarn
Python setup.py pip
Java Maven/Gradle Maven Central

Rustの特徴:統合されたエクスペリエンス

2.2.2 新しいプロジェクトの作成

バイナリプロジェクト(実行可能ファイル):

cargo new hello_world
cd hello_world

生成されるファイル構造:

hello_world/
├── Cargo.toml        # プロジェクト設定
├── .gitignore        # Git設定(自動生成)
└── src/
    └── main.rs       # エントリーポイント

ライブラリプロジェクト

cargo new --lib my_library

生成されるファイル構造:

my_library/
├── Cargo.toml
└── src/
    └── lib.rs        # ライブラリのルート

2.2.3 Cargo.tomlの構造

Cargo.tomlはプロジェクトのマニフェストファイルです(TOML形式):

[package]
name = "hello_world"           # プロジェクト名
version = "0.1.0"              # セマンティックバージョニング
edition = "2021"               # Rustエディション
authors = ["Your Name <you@example.com>"]
description = "A sample Rust project"
license = "MIT"

[dependencies]
# 外部クレートの依存関係
serde = "1.0"                  # 最新の1.x系
tokio = { version = "1.35", features = ["full"] }
regex = "1.10"

[dev-dependencies]
# テスト時のみ使用
criterion = "0.5"              # ベンチマークツール

[build-dependencies]
# ビルドスクリプト時のみ使用
cc = "1.0"

[profile.release]
# リリースビルドの最適化設定
opt-level = 3                  # 最大最適化
lto = true                     # Link Time Optimization
codegen-units = 1              # 単一コード生成ユニット

セマンティックバージョニング

依存関係のバージョン指定
────────────────────────
serde = "1.0"          → ^1.0(1.0以上2.0未満)
serde = "=1.0.195"     → 厳密に1.0.195
serde = ">=1.0, <2.0"  → 範囲指定
serde = "*"            → 最新版(非推奨)

---

2.3 Cargoコマンド

2.3.1 基本コマンド

ビルド

# デバッグビルド(デフォルト)
cargo build

# リリースビルド(最適化有効)
cargo build --release

# 特定のターゲット向け
cargo build --target wasm32-unknown-unknown

ビルド成果物の場所:

target/
├── debug/              # デバッグビルド
│   ├── hello_world     # 実行ファイル
│   └── deps/           # 依存クレート
└── release/            # リリースビルド
    └── hello_world

実行

# ビルドして実行(一発コマンド)
cargo run

# リリースビルドで実行
cargo run --release

# 引数を渡す
cargo run -- arg1 arg2

チェック(コンパイルせず型チェックのみ):

cargo check

# 理由:checkはbuildより高速
# 開発中は頻繁にcheckを実行し、最後にbuild

クリーン

cargo clean  # target/ディレクトリを削除

2.3.2 テストコマンド

ユニットテスト

cargo test

# 特定のテストのみ実行
cargo test test_name

# 並列実行を無効化
cargo test -- --test-threads=1

# 標準出力を表示
cargo test -- --nocapture

ドキュメントテスト

cargo test --doc

ベンチマーク(nightly限定):

cargo +nightly bench

2.3.3 ドキュメント

ドキュメント生成

# プロジェクトのドキュメントを生成
cargo doc

# 依存クレートも含める
cargo doc --no-deps

# ブラウザで開く
cargo doc --open

src/lib.rs):

/// この関数は2つの数値を加算します。
///
/// # Examples
///
/// 
/// let result = my_library::add(2, 3); /// assert_eq!(result, 5); ///
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

2.3.4 コード品質ツール

Rustfmt(フォーマッター)

# コードを自動フォーマット
cargo fmt

# チェックのみ(CI用)
cargo fmt -- --check

設定ファイル rustfmt.toml

max_width = 100
tab_spaces = 4
edition = "2021"

Clippy(リンター)

# リントチェック実行
cargo clippy

# 警告をエラーとして扱う
cargo clippy -- -D warnings

# 自動修正(実験的)
cargo clippy --fix

Clippyの警告例:

// Bad
let x = 5;
if x == 5 {
    // ...
}

// Clippy警告: "redundant pattern matching"
// 改善案を提示してくれる

---

2.4 プロジェクト構造のベストプラクティス

2.4.1 標準的なプロジェクト構造

my_project/
├── Cargo.toml              # プロジェクトマニフェスト
├── Cargo.lock              # 依存関係のロックファイル(バージョン管理に含める)
├── .gitignore              # Gitの除外設定
│
├── src/
│   ├── main.rs             # バイナリのエントリーポイント
│   ├── lib.rs              # ライブラリのルート(オプション)
│   │
│   ├── bin/                # 追加の実行ファイル
│   │   ├── tool1.rs
│   │   └── tool2.rs
│   │
│   └── modules/            # モジュール群
│       ├── mod.rs          # モジュールルート
│       ├── parser.rs
│       └── utils.rs
│
├── tests/                  # 統合テスト
│   ├── integration_test.rs
│   └── common/
│       └── mod.rs          # テスト用ヘルパー
│
├── benches/                # ベンチマーク
│   └── my_benchmark.rs
│
├── examples/               # 使用例
│   └── basic_usage.rs
│
├── docs/                   # 追加ドキュメント
│   └── architecture.md
│
└── target/                 # ビルド成果物(.gitignoreに含める)

2.4.2 モジュール分割の例

lib.rs

// モジュールの宣言
pub mod parser;
pub mod utils;

// 再エクスポート
pub use parser::Parser;
pub use utils::helper_function;

// ライブラリのメイン機能
pub fn run() {
    println!("Library is running!");
}

parser.rs

pub struct Parser {
    // ...
}

impl Parser {
    pub fn new() -> Self {
        Parser { /* ... */ }
    }

    pub fn parse(&self, input: &str) -> Result<(), String> {
        // ...
    }
}

main.rs(lib.rsを使用):

use my_project::{run, Parser};

fn main() {
    run();

    let parser = Parser::new();
    parser.parse("input");
}

2.4.3 ワークスペース

複数のクレートを1つのリポジトリで管理:

ルートのCargo.toml

[workspace]
members = [
    "crate1",
    "crate2",
    "utils",
]

[workspace.dependencies]
# 共通の依存関係
serde = "1.0"
tokio = "1.35"

ディレクトリ構造

my_workspace/
├── Cargo.toml            # ワークスペース設定
├── Cargo.lock            # 共有ロックファイル
│
├── crate1/
│   ├── Cargo.toml
│   └── src/
│       └── main.rs
│
├── crate2/
│   ├── Cargo.toml
│   └── src/
│       └── lib.rs
│
└── target/               # 共有ビルドディレクトリ

ワークスペース内での相互参照

# crate1/Cargo.toml
[dependencies]
crate2 = { path = "../crate2" }

---

2.5 Crates.ioとエコシステム

2.5.1 Crates.ioとは

Crates.ioはRustの公式パッケージレジストリです:

┌─────────────────────────────────────────────────────────┐
│                  Crates.ioエコシステム                    │
├─────────────────────────────────────────────────────────┤
│                                                         │
│   開発者                                                 │
│      │                                                  │
│      │ cargo publish                                    │
│      ▼                                                  │
│   ┌──────────────┐                                      │
│   │  Crates.io   │  ◄─── 150,000+ クレート              │
│   └───────┬──────┘                                      │
│           │                                             │
│           │ cargo add / cargo build                     │
│           ▼                                             │
│      あなたのプロジェクト                                 │
│                                                         │
└─────────────────────────────────────────────────────────┘

統計(2024年):

  • 150,000+ 公開クレート
  • 毎日数百 の新規クレート
  • 100億回以上 のダウンロード

2.5.2 依存クレートの追加

方法1:Cargo.tomlに手動追加

[dependencies]
serde = "1.0"

方法2:cargo-editを使用

# cargo-editのインストール
cargo install cargo-edit

# クレートの追加
cargo add serde

# 特定バージョン
cargo add serde@1.0.195

# フィーチャーフラグ付き
cargo add tokio --features full

方法3:cargo searchで検索

cargo search json

# 出力例:
# serde_json = "1.0"    # JSON serialization
# json = "0.12"         # JSON implementation

2.5.3 重要なクレートカテゴリ

1. シリアライゼーション

serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"      # JSON
serde_yaml = "0.9"      # YAML
toml = "0.8"            # TOML
bincode = "1.3"         # バイナリ

2. 非同期ランタイム

tokio = { version = "1.35", features = ["full"] }
async-std = "1.12"

3. Web フレームワーク

axum = "0.7"            # 最新、Tokio公式
actix-web = "4.4"       # 高性能
rocket = "0.5"          # 使いやすい

4. CLI ツール

clap = { version = "4.4", features = ["derive"] }
structopt = "0.3"

5. エラーハンドリング

anyhow = "1.0"          # シンプルなエラー
thiserror = "1.0"       # カスタムエラー

2.5.4 セキュリティ監査

cargo-auditでセキュリティ脆弱性チェック:

# インストール
cargo install cargo-audit

# 監査実行
cargo audit

# 自動修正
cargo audit fix

---

2.6 ビルドの最適化

2.6.1 リリースプロファイル

Cargo.tomlでプロファイルをカスタマイズ:

[profile.release]
opt-level = 3              # 最適化レベル(0-3, s, z)
lto = "fat"                # Link Time Optimization
codegen-units = 1          # 並列コード生成を無効化(最大最適化)
panic = "abort"            # panicでunwindしない(サイズ削減)
strip = true               # シンボル情報を削除

[profile.dev]
opt-level = 0              # 最適化なし(高速コンパイル)
debug = true               # デバッグ情報を含める

最適化レベルの比較

レベル 説明 ビルド時間 実行速度 バイナリサイズ
0 最適化なし 最速 最遅 大きい
1 基本最適化 速い 速い 中程度
2 デフォルト 中程度 高速 小さい
3 最大最適化 遅い 最速 最小
s サイズ優先 遅い 速い 最小
z 最小サイズ 遅い 中程度 極小

2.6.2 キャッシュの活用

sccacheでコンパイルキャッシュ:

# インストール
cargo install sccache

# 環境変数設定
export RUSTC_WRAPPER=sccache

# キャッシュ統計
sccache --show-stats

効果:

  • 2回目以降のビルドが 50-90%高速化
  • CI/CDでの時間短縮

---

2.7 IDEとエディタの設定

2.7.1 VS Code

推奨拡張機能

{
  "recommendations": [
    "rust-lang.rust-analyzer",    // 公式Rust拡張
    "vadimcn.vscode-lldb",        // デバッガ
    "serayuzgur.crates",          // Cargo.tomlの補完
    "tamasfe.even-better-toml"    // TOML サポート
  ]
}

settings.json

{
  "rust-analyzer.checkOnSave.command": "clippy",
  "rust-analyzer.cargo.allFeatures": true,
  "editor.formatOnSave": true,
  "[rust]": {
    "editor.defaultFormatter": "rust-lang.rust-analyzer"
  }
}

2.7.2 その他のエディタ

Vim/Neovim

  • rust.vim
  • coc-rust-analyzer(CoC)

Emacs

  • rust-mode
  • lsp-mode + rust-analyzer

JetBrains IntelliJ IDEA

  • IntelliJ Rust プラグイン

---

2.8 CI/CDの設定

2.8.1 GitHub Actions

.github/workflows/rust.yml

name: Rust CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Setup Rust
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        components: rustfmt, clippy
        override: true

    - name: Cache cargo registry
      uses: actions/cache@v3
      with:
        path: ~/.cargo/registry
        key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}

    - name: Check formatting
      run: cargo fmt -- --check

    - name: Run clippy
      run: cargo clippy -- -D warnings

    - name: Run tests
      run: cargo test --verbose

    - name: Build release
      run: cargo build --release --verbose

2.8.2 GitLab CI

.gitlab-ci.yml

image: rust:latest

stages:
  - test
  - build

test:
  stage: test
  script:
    - cargo fmt -- --check
    - cargo clippy -- -D warnings
    - cargo test --verbose

build:
  stage: build
  script:
    - cargo build --release
  artifacts:
    paths:
      - target/release/my_binary

---

2.9 まとめ

学んだこと

  • Rustupによるツールチェーン管理
- stable/beta/nightlyの切り替え - クロスコンパイルターゲット

  • Cargoの基本操作
- プロジェクト作成、ビルド、テスト - 依存関係の管理

  • プロジェクト構造
- ベストプラクティス - ワークスペース

  • エコシステム
- Crates.ioの活用 - 重要なクレート

  • 開発環境
- IDEの設定 - CI/CD

次のステップ

次の章では、Rustの基本構文を学びます:

  • 変数と可変性
  • データ型
  • 関数
  • 制御構文

---

参考資料

公式ドキュメント

ツール

コミュニティ