Best practice in specifying dependency versions in Cargo.toml

I see a growing trend of projects that in their Cargo.toml are specifying dependencies using the full triplet:
crate = "1.2.3"
while I would use
crate = "^1.2.3"
Both are equivalent, they allow the adoption of every following version < 2.0.0, but I consider the caret notation explicit and clearer (for 1.0.x I always use "^1").
I've been told that there are IDE plugins that does that.

Are my assumption correct? Am I missing something? Is there any best practices in this area?

You're right that "1.2.3" and "^1.2.3" are equivalent. I prefer the former just to avoid a little symbol noise, but I don't know if there's any trend otherwise.

Do you mean "~1.0"? Because "^1" allows any 1.y.z version.

1 Like

Thanks for your answer
for the 1.0.x I use ^1, because 1.y.z is compatible and I want to express maximum compatibility. I don't like to see the same create, but with different versions, part of the same application. Often times people do not even realize it when it happens.

In general, I prefer to be explicit using the caret notation.
My intuition suggests me that "1.2.3" would mean "=1.2.3", not "^1.2.3".
I was just checking if it's me (very likely) that has a "problem" with that, noticing that many plugins and dependency management tools adopt the explicit triplet without any caret

Note that cargo will never compile multiple semver-compatible versions in the same binary. If you have dependencies on "=1.2.3" and "=1.2.4", then that's just a compile error, and doesn't lead to duplicated dependencies.

1 Like

I didn't know that it's triggering a compile error, good to know! thanks!
Indeed, as package maintainer, I've only seen semver-incompatible versions put on the same application.

It's generally considered good practice to just specify dep = "1.2.3" (or dep = "1.2" if the patch version doesn't matter). This basically tells cargo "I want this version, give me whatever you like." Using convention in this manner is easier in general than requiring everyone to learn what the different semver operators mean, especially since you should always be using ^ anyway, so that's the default scheme.

Thanks, that makes it clear.
"I want this version, but give me whatever you like" explains it beautifully.