github/gitignore · Rust.gitignore
Rust .gitignore 模板详解
说明 Rust.gitignore 忽略 target 目录、rustfmt 备份、MSVC 调试信息和 cargo-mutants 结果的原因,以及建议提交 Cargo.lock 的方针变化。
Rust.gitignore 是只包含 Cargo 项目所需最少规则的简短模板。大部分产物都集中在 target/ 一个目录中,因此规则很少。
旧版模板曾建议库项目忽略 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)时在当前目录留下的堆栈跟踪。报告 bug 时会用到,但仓库中不需要。 |
实践中的注意事项
- 提交
Cargo.lock。为了可复现的构建和 CI 结果的一致性,库也提交它是当前 Cargo 的默认建议。 - 使用工作区时,
target/只会在工作区根目录生成一个,但由于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