Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a command to automatically add a crate to my Cargo.toml?

I expected there to be something like cargo install stopwatch but could not find it in the docs.

I've been finding the package version and manually adding it to Cargo.toml:

[dependencies]
stopwatch = "0.0.6"

But this is tedious and I feel it should be more automated.

like image 695
Johan Larsson Avatar asked Sep 07 '25 22:09

Johan Larsson


2 Answers

As of Rust 1.62.0, there is a built-in add sub-command to add dependencies and avoid having to edit the Cargo.toml file yourself.

To add the latest version:

cargo add stopwatch

To add a specific version:

cargo add [email protected]

More details in the Cargo book.

like image 53
Nerpson Avatar answered Sep 10 '25 09:09

Nerpson


No, there is no such thing built in to Cargo. There is only a cargo install subcommand which installs the binaries of a crate system-wide.

New third-party Cargo subcommands can be created, and cargo edit, does what you want.

These cargo subcommands can then be installed by cargo install, in a fun meta circle!

% cargo install cargo-edit

# Now `cargo add` is available
% cargo add mycrate
like image 43
Shepmaster Avatar answered Sep 10 '25 07:09

Shepmaster