Add a crate with same version as a dependency is using?

I’m not even sure exactly how to say this clearly in word without it being potentially confusing, so here is an example.

Ex. I’m using crate A.
Crate A depends on crate B.
I realize I need crate B also, can I somehow specify that I want to use whatever version of B that A is using?

Afaik that isn’t possible. The inner dependency isn’t in your control. What you can do is use the appropriate version of A that uses the version of B you want.

You can use very wide semver range and let Cargo figure it out

b = "1"

Or you can make crate A re-export B (pub use B) and use A::B.

I want the opposite of this, I want to use the same version of B as A is already using.

If cargo will auto use the same version that might be an okay solution. Crate A is from crates.io, so I don’t control whether it re-exports B.

Edit: Using a wide semver isn’t working, my crate B requirement is completely ignoring crate A’s and just following semver.

so you want to add another dependency B, which is also used by A, and make both depend on the same version?

This isn’t in TOML format, but what I want is:

My Crate {
    A {
        B = 1.2.1
    }
    B = <whatever version of B that A is using (so 1.2.1 in this example)>
}
  • In Cargo.toml "1.2.1" means any version between 1.2.1 and 1.9999.9999.
  • Cargo tries to have only one version of every crate, so unless you set incompatible version, your crate and crate A will get the same one version of B.

So if A::B is set to “1.2.1”, you can also set “1.2.1” or “1.0.0”, or “1.*” in your Cargo.toml. It’s likely that you will get some other version (like 1.3.5), but both crates will share the dependency. Unless A::B is set to 1.2.1 and your create requires 2.0.0 or 0.0.1.

It’s not doing that. The version of B that A is using is 0.18.0. If I do B = "0.18.0" then I get 0.18.0 and my code works. If I try “0” or “~0” or “^0” or “*”, (all of which are semver compatible with 0.18.0 I believe), then I get the wrong version, and while compiling it shows

Compiling B v0.18.0
Compiling B v0.19.0

Versions >= 1.0.0 are major.minor.patch, versions < 1.0.0 are 0.major.minor

So 0.18 and 0.19 are incompatible, as if they were 18.0.0 and 19.0.0.

Oh, so your saying cargo will only match the crate versions if the dependency requirement I specify only allows the minor or patch numbers to change?

I knew that 0.18 and 0.19 would be a different major, I was just hoping cargo would match the versions :frowning:

It’s all explained in https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html . You can write "b" = ">=0" but that’s probably not actually correct, since you’ll likely not be compatible with all of those.

I’ve read the specifying dependencies page, but it doesn’t have anything on how to do what I want (Have crate B use the same version as crate A’s B uses), as far as I could see.

Look at the problem from the other direction. Go through all the versions of A and note down which version of B it uses. Then limit A to only those versions that use the same B as you are using directly.