1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WindowsにRustをインストールしてプロジェクトを作成するまで

1
Posted at

WindowsにRustをインストールしてプロジェクトを作成するまで

Rustの公式ツールチェインをWindowsにインストールし、Cargoで新規プロジェクトを作成して実行するまでの手順をまとめます。

対象環境

  • OS: Windows 10 / 11
  • インストール方法: rustup(Rust公式のバージョン管理ツール)

1. Visual Studio C++ Build Toolsのインストール

RustのWindows向け標準ツールチェイン(x86_64-pc-windows-msvc)はMSVCのリンカが必要なため、事前に「Visual Studio Build Tools」をインストールします。

  1. Visual Studio Build Tools をダウンロード
  2. インストーラーを起動し、ワークロードから 「C++によるデスクトップ開発」 にチェックを入れてインストール

すでにVisual Studioが入っている場合はこの手順は不要です。

2. rustupのインストール

  1. Install Rust - Rust Programming Language にアクセスし、rustup-init.exe をダウンロード
  2. ダウンロードしたインストーラーを実行
rustup-init.exe
  1. インストールオプションを聞かれるので、特にこだわりがなければ 1) Proceed with standard installation (default - just press enter) を選択

インストールが完了すると、以下のツールが使えるようになります。

コマンド 役割
rustc Rustコンパイラ
cargo パッケージ管理・ビルドツール
rustup ツールチェインのバージョン管理

3. インストール確認

インストール後、ターミナルを再起動してからバージョンを確認します。

rustc --version
cargo --version

以下のようにバージョンが表示されればインストール成功です。

rustc 1.98.1 (48a229cea 2026-09-01)
cargo 1.98.1 (797e8a9bc 2026-08-05)

4. Rustのアップデート(手動)

Rustは頻繁にバージョンが更新されるため、rustup コマンドで最新版に手動アップデートできます。

rustup update

更新後は、バージョンが上がっていることを確認します。

rustc --version
cargo --version

なお、現在インストールされているツールチェインの一覧やアップデート状況は以下のコマンドで確認できます。

rustup show

5. Cargoで新規プロジェクトを作成

Rustではプロジェクトの作成・ビルド・依存関係の管理を cargo コマンドで行います。

cargo new hello_cargo

実行すると、以下のようなファイル構成のプロジェクトが生成されます。

hello_cargo/
├── Cargo.toml
└── src/
    └── main.rs

--vcs オプションのデフォルト値は git ですが、すでにGit管理下にあるディレクトリ内で実行した場合は自動検出され、Gitリポジトリの初期化(.git / .gitignore の生成)はスキップされます。今回のように、LearnRust フォルダ自体がGitリポジトリになっているケースがこれに該当します。

実際に検証すると、違いがわかります。

cargo new sample1              # --vcsを指定しない
cargo new sample2 --vcs git    # 明示的にgitを指定
プロジェクト コマンド 結果
sample1 --vcs なし 既存のGitリポジトリ内のため初期化されず、.git / .gitignore なし
sample2 --vcs git 明示的に指定したため、ネストした状態でも .git / .gitignore が生成される

Gitで管理しないことを明示したい場合は --vcs none を指定します。

cargo new hello_cargo --vcs none

--vcs オプションには git(デフォルト)、none のほか hg(Mercurial)、pijul、fossil も指定できます。

Cargo.toml にはプロジェクト名やRustのエディション情報が記載されています。

[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2024"

[dependencies]

src/main.rs にはデフォルトで「Hello, world!」を出力するコードが生成されています。

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

6. プロジェクトの実行

作成したプロジェクトのディレクトリに移動し、cargo run でビルドと実行を同時に行います。

cd hello_cargo
cargo run

以下のように出力されれば成功です。

   Compiling hello_cargo v0.1.0 (C:\path\to\hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.5s
     Running `target\debug\hello_cargo.exe`
Hello, world!

なお、ビルドのみ行いたい場合は cargo build、コンパイルエラーの確認だけであれば cargo check が高速で便利です。

まとめ

  • WindowsでRustを使うには、事前にVisual Studio Build Toolsの「C++によるデスクトップ開発」が必要
  • rustup を使うことでRust公式ツールチェイン(rustc / cargo)を簡単にインストールできる
  • rustup update でRustのツールチェインを最新版に手動アップデートできる
  • cargo new でプロジェクトを作成し、cargo run でビルド&実行までワンコマンドで完結する

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?