Cargo 项目中的哪些文件应该在我的 .gitignore 中?
What files in a Cargo project should be in my .gitignore?
我使用 cargo new
创建了一个 "hello world" Rust 应用程序。当我执行 git status
时,它显示了一堆文件:
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
我可以安全地忽略这些文件中的任何一个 and/or 目录吗?
总结
.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
用于可执行文件箱
# Generated files
/target/
详情
您的 .gitignore
中需要一个或两个条目,具体取决于您构建的 crate 类型。 target/
文件夹可以完全忽略,无论 crate 类型如何;它只包含生成的文件(例如编译工件)。
Cargo.lock
文件应该包含在存储库中 如果 您正在编写可执行文件,如果您正在编写库,则应该忽略该文件。您可以阅读有关此内容的更多信息 in the FAQ。引用最重要的部分:
The purpose of a Cargo.lock
is to describe the state of the world at the time of a successful build. [...]
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
.
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
通过了。
您可以从 GitHub's gitignore for Rust 中获得一些灵感。在撰写本文时,文件如下:
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
我使用 cargo new
创建了一个 "hello world" Rust 应用程序。当我执行 git status
时,它显示了一堆文件:
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
我可以安全地忽略这些文件中的任何一个 and/or 目录吗?
总结
.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
用于可执行文件箱
# Generated files
/target/
详情
您的 .gitignore
中需要一个或两个条目,具体取决于您构建的 crate 类型。 target/
文件夹可以完全忽略,无论 crate 类型如何;它只包含生成的文件(例如编译工件)。
Cargo.lock
文件应该包含在存储库中 如果 您正在编写可执行文件,如果您正在编写库,则应该忽略该文件。您可以阅读有关此内容的更多信息 in the FAQ。引用最重要的部分:
The purpose of a
Cargo.lock
is to describe the state of the world at the time of a successful build. [...]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
.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
通过了。
您可以从 GitHub's gitignore for Rust 中获得一些灵感。在撰写本文时,文件如下:
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb