Quick, automated Cargo dependencies
2020.01.12 RustFor a long time, I have been adding cargo dependencies one by one, using
crates.io to copy and paste a version string to copy and
paste into Cargo.toml
.
Recently, however, I found a quick and simple solution to sidestep this unnecessary effort, using the excellent cargo-edit.
Once installed with
cargo install cargo-edit
...cargo edit
installs cargo commands, including cargo upgrade
, which handily
upgrades all of the dependencies in a Cargo.toml
. This is nice on its own,
and something I was looking for for a while anyway.
EDIT: previously missing the blindingly obvious, cargo-edit
also provides
cargo add <crate>
which achieves exactly the same thing as below.
A nice side effect of this beautiful command, however, is that you can add a new
dependency, e.g. dirs
, with an "any"/*
dependency:
[dependencies] aocf = {version = "0.1.1", path = ".."} chrono = "0.4.10" docopt = "1.1.0" dirs = "*" failure = "0.1.6" serde = "1.0.104" serde_derive = "1.0.104" tempfile = "3.1.0" toml = "0.5.5"
And then after running cargo upgrade
, you are magically left with the latest
version of the crate from crates.io
:
[dependencies] aocf = {version = "0.1.1", path = ".."} chrono = "0.4.10" docopt = "1.1.0" dirs = "2.0.2" failure = "0.1.6" serde = "1.0.104" serde_derive = "1.0.104" tempfile = "3.1.0" toml = "0.5.5"
Clearly this works best if you keep all of your other dependencies up to date,
but I think this is either a) good practice, or b) you could always use git add -p
.
Alternatively, in this case, you can use:
cargo upgrade dirs
...and it will only upgrade the dirs
dependency.