2
votes

Today I spent more time than I should have trying to figure out why my project wasn't building, only to find out that I forgot to include the following crate:

[dependencies]
glob = "~0.3.0"

I'd like to avoid making this mistake again by having Cargo add the name/version of a package to the [dependencies] section of my Cargo.toml file when I install the package.

To give you a better example of what I mean, in NPM if you run:

npm install --save-dev glob

It'd save the name/version of the glob package to dependencies section of the package.json file.

How do I do this in Cargo?

1

1 Answers

3
votes

There's a crate called cargo-edit which expands cargo with subcommands add, rm and upgrade to act similarly to how npm install (and other package managers) do:

# install cargo-edit
cargo install cargo-edit

# add crate "glob"
cargo add glob

The resulting Cargo.toml file would look like this:

[dependencies]
glob = "0.3.0"

The crate will be downloaded and built the next time you run a regular Cargo command (e.g. cargo build, cargo run, cargo test).