Cargo 项目中的哪些文件应该在我的 .gitignore 中?
我使用 cargo new
创建了一个hello world"Rust 应用程序.当我执行 git status
时,它显示了一堆文件:
I created a "hello world" Rust app using cargo new
. When I executed git status
it showed a bunch of files:
A rust/welcomec/Cargo.lock
A rust/welcomec/Cargo.toml
A rust/welcomec/src/main.rs
A rust/welcomec/target/debug/.cargo-lock
A rust/welcomec/target/debug/.fingerprint/welcomec-2d68725c8fae6fd1/bin-welcome-2d68725c8fae6fd1
A rust/welcomec/target/debug/.fingerprint/welcomec-2d68725c8fae6fd1/bin-welcome-2d68725c8fae6fd1.json
A rust/welcomec/target/debug/.fingerprint/welcomec-2d68725c8fae6fd1/dep-bin-welcome-2d68725c8fae6fd1
A rust/welcomec/target/debug/deps/welcome-2d68725c8fae6fd1
A rust/welcomec/target/debug/welcome
A rust/welcomec/target/debug/welcome.d
我可以安全地忽略任何这些文件和/或目录吗?
Can I safely ignore any of these files and/or directories?
总结
.gitignore
用于库包
# Generated files
/target/
# The library shouldn't decide about the exact versions of
# its dependencies, but let the downstream crate decide.
Cargo.lock
.gitignore
用于可执行包
.gitignore
for executable crates
# Generated files
/target/
详情
您的 .gitignore
中需要一两个条目,具体取决于您正在构建的板条箱类型.target/
文件夹可以完全忽略,无论 crate 类型如何;它只包含生成的文件(例如编译工件).
Details
You need one or two entries in your .gitignore
, depending on what kind of crate you're building. The target/
folder can be ignore completely regardless of crate type; it only contains generated files (e.g. compile artifacts).
Cargo.lock
文件应该包含在存储库中如果你正在编写一个可执行文件,并且如果你正在编写一个库应该被忽略.您可以阅读有关此内容的更多信息 在常见问题解答中.引用最重要的部分:
The Cargo.lock
file should be included in the repository if you're writing an executable, and should be ignored if you're writing a library. You can read more about this in the FAQ. To quote the most important part:
Cargo.lock
的目的是描述成功构建时的世界状态.[...]
The purpose of a
Cargo.lock
is to describe the state of the world at the time of a successful build. [...]
这个属性最适合位于依赖链(二进制文件)末端的应用程序和项目.因此,建议所有二进制文件都检查其 Cargo.lock
.
This property is most desirable from applications and projects which are at the very end of the dependency chain (binaries). As a result, it is recommended that all binaries check in their Cargo.lock
.
对于图书馆,情况有些不同.[...] 如果一个库最终被多个依赖项传递使用,很可能只需要该库的一个副本(基于 semver 兼容性).如果所有库都签入它们的 Cargo.lock
,那么将使用该库的多个副本,甚至可能会发生版本冲突.
For libraries the situation is somewhat different. [...] If a library ends up being used transitively by several dependencies, it’s likely that just a single copy of the library is desired (based on semver compatibility). If all libraries were to check in their Cargo.lock
, then multiple copies of the library would be used, and perhaps even a version conflict.
另外请注意cargo new
和cargo init
在项目中自动生成.gitignore
文件, 除非参数 --vcs none
被传递.