Search code examples
rustrust-cargo

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.


Solution

  • 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