Cargo项目中的哪些文件应该在我的.gitignore中?

lab*_*nth 3 rust rust-cargo

我创建了一个"hello world"Rust应用程序cargo new.当我执行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
Run Code Online (Sandbox Code Playgroud)

我可以安全地忽略这些文件和/或目录吗?

rad*_*row 7

您可以从GitHub 的 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
Run Code Online (Sandbox Code Playgroud)


Luk*_*odt 6

摘要

.gitignore 对于图书馆板条箱

# Generated files
/target/

# The library shouldn't decide about the exact versions of 
# its dependencies, but let the downstream crate decide.
Cargo.lock
Run Code Online (Sandbox Code Playgroud)

.gitignore 对于可执行的板条箱

# Generated files
/target/
Run Code Online (Sandbox Code Playgroud)

细节

您需要一个或两个条目.gitignore,具体取决于您正在构建的箱子类型.target/无论包装类型如何,都可以完全忽略该文件夹; 它只包含生成的文件(例如编译工件).

如果您正在编写可执行文件,Cargo.lock文件应包含在存储库中,如果您正在编写库,则应忽略该文件.您可以在常见问题中了解更多相关信息.引用最重要的部分:

a的目的Cargo.lock是描述成功构建时的世界状态.[...]

从属于依赖关系链(二进制文件)末尾的应用程序和项目中,最需要此属性.因此,建议所有二进制文件检查其中Cargo.lock.

对于图书馆来说,情况有所不同.[...]如果一个库最终被多个依赖项传递使用,那么很可能只需要一个库的副本(基于semver兼容性).如果所有库都要检查它们Cargo.lock,那么将使用该库的多个副本,甚至可能是版本冲突.


此外,请注意cargo new并在项目中cargo init 自动生成.gitignore文件,除非--vcs none传递参数.

  • 我要补充的一件事是,如果您在现有 git 存储库中创建子目录,它不会自动生成 .gitignore 文件 (5认同)