๐ฆ Rust - Dependencies
You define dependencies in the Cargo.toml
file.
[dependencies]
rand = "0.8.4"
With your own applications, you can be pretty loose how you define your dependencies as you have no downstream to break:
# the three-digit format is common
rand = "0.8.4"
# "0.8.4" is the same as "^0.8.4"
# so any version >= 0.8.4 and < 0.9.0
# which allows getting minor updates and bugfixes
You will only be breaking your own builds.
With published libraries, you need to be a bit more disciplined:
# some devs favor "two-digit rule" which makes it easy to communicate what you support
rand = "0.8"
# meaning more clearly defined as
rand = ">=0.8.0, <0.9"
# but this doesn't ofc always apply as you might rely on a minor version feature
# use ranges to avoid ambiguity
rand = ">=0.8.4, <0.9"
# for an exact version, communicating "it only works with this one"
rand = "=0.8.4"
You should always test your libraries against the latest you support.
You may test libraries against the minimum versions you support. You can get minimal versions withcargo update -Zdirect-minimal-versions
in CI.
If you have issues with indirect dependencies, you can fine which of your dependencies are causing the issue with cargo tree
:
# let's say I have issues with the `egui` crate being multiple times
cargo tree -i egui
# it should show which of your dependencies depend on which `egui`
# then you drill down to see which versions conflict and if you can fix it
cargo tree -i egui@0.29.1
# egui v0.29.1
# โโโ bevy_egui v0.31.1
# โโโ leafwing-input-manager v0.16.0
# โโโ my-crate v0.1.0 (/home/ruksi/Projects/my-workspace/my-crate)
cargo tree -i egui@0.30.0
#egui v0.30.0
#โโโ bevy_egui v0.32.0
# โโโ my-crate v0.1.0 (/home/ruksi/Projects/my-workspace/my-crate)
If you really have to, you can force a specific version of a library:
In general, it is better to try to find a combination of dependency versions that are specified to work together.
[patch.crates-io]
egui = "=0.30.0"