Rust .gitignore template
The Rust template ignores the target build directory and debug symbols, and deliberately ships the Cargo.lock decision as a comment rather than a rule, because it is a judgement call.
At a glance
- Category
- Language
- Patterns in this template
- 4 rules, plus 1 comment line
- Directories ignored
- 2 of the 4 rules end in a slash, so they match directories only
- Also needs an OS template
- Yes. Rust puts almost everything in target, so the rest of your noise comes from the editor and the file manager.
- Typical companions
- An editor template, an OS template, and Docker if you build release images.
In detail
target is the whole story. Every compiled dependency, every intermediate artefact, the incremental compilation database and the final binaries all live there, and on a project with a moderate dependency tree it runs to gigabytes. It is also machine specific: it contains absolute paths and objects built for one target triple. Nothing in it is worth carrying and all of it is reproducible from the sources plus the lockfile.
debug is a legacy sibling of that rule. Older project layouts wrote debug builds to a top level directory of that name, and modern cargo puts them under target/debug instead. Keeping the line costs nothing, but it will also match a source directory called debug if you ever create one.
Cargo.lock is the deliberate judgement call, and the template makes it visible by shipping it as a comment rather than a rule, so by default the lockfile stays tracked. For an application or any binary you distribute, that is what you want: the lockfile pins exact versions so a build today and a build in six months produce the same dependency set. For a library, the lockfile is ignored by anyone who depends on you, so committing it only pins what your own CI tests against.
That second case is why the comment exists rather than a decision being made for you. The modern advice has drifted towards committing it even for libraries, on the grounds that a reproducible CI run is worth more than the mild dishonesty of a lockfile downstream users never see. Either way it should be a decision someone made on purpose.
The remaining two rules are small. Files ending in .rs.bk are backup files from an old rustfmt, and *.pdb files are the separate debug symbol files produced on Windows, which are large, uninteresting and regenerated on every build. Neither is likely to appear in a project started recently, but both cost nothing to keep, and the symbol file rule in particular saves a Windows contributor from committing something several times the size of the executable it describes.
Examples
debug/
target/
# Uncomment for libraries: Cargo.lock
**/*.rs.bk
*.pdb5 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- Uncommenting the Cargo.lock line in a workspace removes the lockfile for every member crate at once, including the binary crates where you almost certainly wanted to keep it.
- debug/ is not anchored, so it matches at any depth. In a repository that keeps sample data under assets/debug, that data quietly disappears from git.
- Deleting target to reclaim disk space is safe, but cargo will rebuild every dependency from scratch afterwards. Use cargo clean if you want the same effect without reaching for rm.
Try it: .gitignore Generator
Pick one or more templates, tick the common additions you want, and copy or download the combined .gitignore.
.gitignore Generator
LOCAL · Browser-OnlyGenerate comprehensive .gitignore files by combining templates for languages, frameworks, IDEs, and operating systems
Language
Framework
IDE
OS
Build Tool
Infrastructure
Common Additions
Preview
Select templates or common additions to generate a .gitignore file.
Related .gitignore templates
All .gitignore templates →Go .gitignore template
The Go template ignores platform binaries by extension, which misses the extensionless binary go build produces on Linux and macOS. Here is what to add.
C .gitignore template
What the C .gitignore template covers: object files, static and shared libraries, linker maps, debug symbols and the patterns that swallow a vendored binary.
C++ .gitignore template
The C++ template adds CMake output to the usual compiler artefacts, and its Makefile rule will ignore a hand written Makefile. Here is what each line covers.
C# .gitignore template
Why the C# template writes [Bb]in instead of bin, what the NuGet and Visual Studio rules cover, and which per developer files it keeps out of the repository.
Ruby .gitignore template
The Ruby template anchors most rules to the repository root with a leading slash, and ignores Gemfile.lock, which is right for a gem and wrong for an app.
PHP .gitignore template
The PHP template ignores the vendor directory and the .env file, and also ignores composer.lock, which most application projects should commit instead.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS
- Changelog Generator Generate changelogs in Keep a Changelog format - version entries, semver, and diff links