← 返回命令列表

Linux command

cargo-add 命令

安全

权限或系统影响较大,执行前请核对目标。

常用示例

Add dependency

cargo add [serde]

Add with specific version

cargo add [serde@1.0]

Add with features

cargo add [tokio] --features [full]

Add as dev dependency

cargo add [mockall] --dev

Add as build dependency

cargo add [cc] --build

Add from git repository

cargo add --git [https://github.com/user/repo]

Add from local path

cargo add --path [../my-crate]

Add as optional dependency

cargo add [feature-crate] --optional

Rename dependency

cargo add [serde] --rename [my_serde]

说明

cargo add adds or modifies dependencies in Cargo.toml. It fetches crate information from the registry and updates the manifest with the correct version constraints. Built into Cargo since v1.62.

参数

--dev, -D
Add as development dependency
--build, -B
Add as build dependency
--optional
Mark dependency as optional
--rename _name_
Rename the dependency
--features _features_
Enable specific features
--no-default-features
Disable default features
--default-features
Re-enable default features
--path _path_
Add dependency from local path
--git _url_
Add dependency from git repository
--branch _branch_
Git branch to use
--tag _tag_
Git tag to use
--rev _sha_
Git commit to use
-p, --package _spec_
Package to modify
--dry-run
Show changes without modifying files

FAQ

What is the cargo-add command used for?

cargo add adds or modifies dependencies in Cargo.toml. It fetches crate information from the registry and updates the manifest with the correct version constraints. Built into Cargo since v1.62.

How do I run a basic cargo-add example?

Run `cargo add [serde]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does --dev, -D do in cargo-add?

Add as development dependency