22

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

Finding the package version and manually adding the package to .toml:

[dependencies]
stopwatch="0.0.6"

Does not feel automated enough. :)

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Johan Larsson
  • 17,112
  • 9
  • 74
  • 88
  • 1
    this is an [update](https://github.com/rust-lang/cargo/issues/5586) on that matter, in case someone is also wondering if the answer is outdated – ignacio Dec 11 '21 at 10:59

2 Answers2

25

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
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
21

As of Rust 1.62.0, you can use the following command to add dependencies, avoiding you to open the .toml file.

cargo add dependency@version

More info here: https://doc.rust-lang.org/nightly/cargo/commands/cargo-add.html

Nerpson
  • 382
  • 2
  • 13