github/gitignore · Rust.gitignore
Rust .gitignore テンプレート解説
Rust.gitignore が target ディレクトリ、rustfmt のバックアップ、MSVC のデバッグ情報、cargo-mutants の結果を無視する理由と、Cargo.lock のコミットを推奨する方針変更を説明します。
Rust.gitignore は Cargo プロジェクトに必要な最小限のルールだけを含む短いテンプレートである。ほとんどの成果物が target/ 1 つに集まるため、ルール数が少ない。
以前のテンプレートはライブラリの場合 Cargo.lock を無視するよう案内していたが、現在のテンプレートにそのルールはない。Rust チームは 2023 年に推奨事項を変更し、ライブラリを含むすべてのプロジェクトで Cargo.lock のコミットを基本とした。
ルール別の解説
| パターン | 何を、なぜ無視するか |
|---|---|
targetdebug | Cargo ビルド結果cargo build がコンパイル結果、インクリメンタルビルドのキャッシュ、依存関係のビルド物を入れるフォルダーである。数 GB にまで大きくなることがあり、完全に再生成される。末尾に / がないため、同名のファイルも無視される。 |
**/*.rs.bk | rustfmt バックアップrustfmt がファイルを修正する前に残すバックアップである。**/ で始まるため、あらゆる深さで一致する。 |
*.pdb | MSVC デバッグ情報Windows の MSVC ツールチェーンでビルドするときに生成されるデバッグシンボルのデータベースである。 |
**/mutants.out*/ | ミューテーションテスト結果cargo-mutants が作る結果ディレクトリ(mutants.out、mutants.out.old)である。末尾の / によりディレクトリにのみ一致する。 |
rustc-ice-*.txt | コンパイラー内部エラーのダンプrustc が内部コンパイラーエラー(ICE)に遭遇したときにカレントディレクトリへ残すスタックトレースである。バグ報告には使うが、リポジトリには不要である。 |
実務での注意点
Cargo.lockはコミットする。再現可能なビルドと CI 結果の一貫性のため、ライブラリもコミットするのが現在の Cargo の基本的な推奨である。- ワークスペースを使うと
target/はワークスペースルートに 1 つだけ作られるが、スラッシュのないtargetルールなので、どの位置に作られても無視される。 - RustRover・IntelliJ の設定は JetBrains テンプレートで別に扱う。テンプレートのコメントも
.idea/全体を無視する方法は推奨しないと記している。
元のテンプレート
Rust.gitignore
# Generated by Cargo# will have compiled files and executablesdebugtarget# These are backup files generated by rustfmt**/*.rs.bk# MSVC Windows builds of rustc generate these, which store debugging information*.pdb# Generated by cargo mutants# Contains mutation testing data**/mutants.out*/# rustc will dump stack traces when hitting an internal compiler error to PWDrustc-ice-*.txt# RustRover# JetBrains specific template is maintained in a separate JetBrains.gitignore that can# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore# and can be added to the global gitignore or merged into this file. For a more nuclear# option (not recommended) you can uncomment the following to ignore the entire idea folder.#.idea/
テンプレートの出典: github/gitignore/Rust.gitignore @356fd7b (2026-09-11) · CC0-1.0