github/gitignore · Rust.gitignore

Rust .gitignore 模板详解

说明 Rust.gitignore 忽略 target 目录、rustfmt 备份、MSVC 调试信息和 cargo-mutants 结果的原因,以及建议提交 Cargo.lock 的方针变化。

在生成器中打开 与 VisualStudioCode, JetBrains, macOS 一起打开

Rust.gitignore 是只包含 Cargo 项目所需最少规则的简短模板。大部分产物都集中在 target/ 一个目录中,因此规则很少。

旧版模板曾建议库项目忽略 Cargo.lock,但当前模板中已没有这条规则。Rust 团队在 2023 年修改了建议,把提交 Cargo.lock 作为包括库在内所有项目的默认做法。

逐条规则详解

模式忽略什么,为什么忽略
targetdebugCargo 构建结果cargo build 存放编译结果、增量构建缓存和依赖构建产物的目录。它可能增长到数 GB,并且可以完全重新生成。末尾没有 /,因此同名文件也会被忽略。
**/*.rs.bkrustfmt 备份rustfmt 修改文件前留下的备份。以 **/ 开头,因此在任意深度都会匹配。
*.pdbMSVC 调试信息使用 Windows MSVC 工具链构建时生成的调试符号数据库。
**/mutants.out*/变异测试结果cargo-mutants 生成的结果目录(mutants.outmutants.out.old)。由于末尾的 /,只匹配目录。
rustc-ice-*.txt编译器内部错误转储rustc 遇到内部编译器错误(ICE)时在当前目录留下的堆栈跟踪。报告 bug 时会用到,但仓库中不需要。

实践中的注意事项

原始模板

Rust.gitignore规则 6 行
# 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

常搭配使用的模板

其他模板详解