Import version of dependency which other dependency requires

Viewed 789

I have two crates X and Z and I want to use both but X depends on Z of on some specific version. For example, mongodb depends on tokio 0.2 and I was using tokio 0.3.

X does not re-export Z so I (think) have to manually specify Z in Cargo.toml. If I specify the wrong version, it might break. How can I tell cargo to use whatever version of Z that X requires?

1 Answers

Cargo has some automatic dependency version resolution capability. You should be able to specify whatever version requirements of Z you need, and cargo will figure out how to choose the correct version to (potentially) use in both your code and X. This is assuming both crates correctly use semantic versioning.

If X doesn't expose Z in any way, you don't really need to use the same version of Z. If you use a different major version of Z than X, Cargo will just use both versions of Z, once for you and once for X.

See alexcrichton's comments on this for a little bit more detail.

Related