cargo
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCargo
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: is built-in.
cargo test
- Rust项目:必备工具。
- 格式化/代码检查:使用、
cargo fmt。cargo clippy - 测试:内置功能。
cargo test
Quick Start
快速开始
bash
cargo new my-project
cd my-project
cargo runtoml
undefinedbash
cargo new my-project
cd my-project
cargo runtoml
undefinedCargo.toml
Cargo.toml
[dependencies]
serde = "1.0"
undefined[dependencies]
serde = "1.0"
undefinedCore 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. in root .
[workspace]Cargo.toml用于在单个仓库中管理多个包。在根目录的中添加配置即可使用。
Cargo.toml[workspace]Best Practices (2025)
2025年最佳实践
Do:
- Use : Faster than
cargo checkfor checking syntax during dev.build - Use : Listen to the linter. It teaches you Rust.
clippy - Use : Scan dependency tree for licenses and bans.
cargo-deny
Don't:
- Don't bloat: Rust binaries can get huge. Use to analyze size.
cargo-bloat
建议做法:
- 使用:在开发过程中,它比
cargo check更快,可用于语法检查。cargo build - 使用:遵循该代码检查工具的建议,它能帮助你更好地掌握Rust。
clippy - 使用:扫描依赖树,检查许可证合规性及禁用依赖。
cargo-deny
不建议做法:
- 不要过度膨胀:Rust二进制文件可能会变得很大,可使用分析文件大小。
cargo-bloat