cargo

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cargo

Cargo

Cargo is Rust's build system and package manager. It is famous for its reliability and developer experience.
Cargo是Rust的构建系统和包管理器,以其可靠性和出色的开发者体验著称。

When to Use

使用场景

  • Rust Projects: Mandatory.
  • Formatting/Linting:
    cargo fmt
    ,
    cargo clippy
    .
  • Testing:
    cargo test
    is built-in.
  • Rust项目:必备工具。
  • 格式化/代码检查:使用
    cargo fmt
    cargo clippy
  • 测试:内置
    cargo test
    功能。

Quick Start

快速开始

bash
cargo new my-project
cd my-project
cargo run
toml
undefined
bash
cargo new my-project
cd my-project
cargo run
toml
undefined

Cargo.toml

Cargo.toml

[dependencies] serde = "1.0"
undefined
[dependencies] serde = "1.0"
undefined

Core Concepts

核心概念

Crates

Crates

Packages. Published to crates.io.
即Rust包,可发布至crates.io平台。

Cargo.lock

Cargo.lock

Ensures reproducible builds. Always commit for binaries, ignore for libraries.
用于确保构建的可复现性。对于二进制项目,务必提交该文件;对于库项目,则建议忽略。

Workspaces

Workspaces

Manage multiple packages in one repo.
[workspace]
in root
Cargo.toml
.
用于在单个仓库中管理多个包。在根目录的
Cargo.toml
中添加
[workspace]
配置即可使用。

Best Practices (2025)

2025年最佳实践

Do:
  • Use
    cargo check
    : Faster than
    build
    for checking syntax during dev.
  • Use
    clippy
    : Listen to the linter. It teaches you Rust.
  • Use
    cargo-deny
    : Scan dependency tree for licenses and bans.
Don't:
  • Don't bloat: Rust binaries can get huge. Use
    cargo-bloat
    to analyze size.
建议做法
  • 使用
    cargo check
    :在开发过程中,它比
    cargo build
    更快,可用于语法检查。
  • 使用
    clippy
    :遵循该代码检查工具的建议,它能帮助你更好地掌握Rust。
  • 使用
    cargo-deny
    :扫描依赖树,检查许可证合规性及禁用依赖。
不建议做法
  • 不要过度膨胀:Rust二进制文件可能会变得很大,可使用
    cargo-bloat
    分析文件大小。

References

参考资料