10aday › Version Constraint Checker › Cargo.toml version requirements

Cargo.toml version requirements: what "1.2.3", ^, ~, * and = really allow

Short answer: in Cargo.toml a bare version is a caret requirement: serde = "1.2.3" allows ≥1.2.3 <2.0.0, and rand = "0.8.5" allows ≥0.8.5 <0.9.0. To pin one version, write "=1.2.3". npm reads the same bare string as an exact version, which is why copying requirements between package.json and Cargo.toml changes their meaning.

Have a constraint and a list of versions? Paste them into the Version Constraint Checker: it shows, side by side, what npm, Cargo, Composer, pip and RubyGems accept.

Every requirement form, Cargo vs npm

Highlighted: npm reads the same string differently.

ConstraintCargonpm
"1.2.3" (bare)≥1.2.3 <2.0.0exactly 1.2.3try it
"1.2" (bare)≥1.2.0 <2.0.0≥1.2.0 <1.3.0try it
"1" (bare)≥1.0.0 <2.0.0≥1.0.0 <2.0.0try it
"0.2.3" (bare)≥0.2.3 <0.3.0exactly 0.2.3try it
"0.0.3" (bare)≥0.0.3 <0.0.4exactly 0.0.3try it
^1.2.3≥1.2.3 <2.0.0≥1.2.3 <2.0.0try it
^0.2.3≥0.2.3 <0.3.0≥0.2.3 <0.3.0try it
~1.2.3≥1.2.3 <1.3.0≥1.2.3 <1.3.0try it
~1.2≥1.2.0 <1.3.0≥1.2.0 <1.3.0try it
~1≥1.0.0 <2.0.0≥1.0.0 <2.0.0try it
*any versionany versiontry it
1.*≥1.0.0 <2.0.0≥1.0.0 <2.0.0try it
1.2.*≥1.2.0 <1.3.0≥1.2.0 <1.3.0try it
=1.2.3exactly 1.2.3exactly 1.2.3try it
=1.2
not exact!
≥1.2.0 <1.3.0≥1.2.0 <1.3.0try it
>1.2≥1.3.0≥1.3.0try it
<=2.3<2.4.0<2.4.0try it
>=1.2, <1.5
npm uses a space, not a comma
>=1.2, <1.5
≥1.2.0 <1.5.0
>=1.2 <1.5
≥1.2.0 <1.5.0
try it

Ranges shown for release versions. Cargo only matches a pre-release such as 1.3.0-rc.1 when the requirement itself names a pre-release on the same version; see the checker.

Rules of thumb

Common questions

What does serde = "1.0" mean in Cargo.toml?

It is the caret requirement ^1.0: any version ≥1.0.0 and <2.0.0. Cargo will pick the newest compatible 1.x release when it resolves the lock file.

How do I pin an exact version in Cargo.toml?

Write "=1.2.3". Note that "=1.2" is not exact: it allows every 1.2.x (≥1.2.0 <1.3.0).

Why does "0.8.5" not accept 0.9.0 in Cargo?

Below 1.0.0 the caret keeps the first non-zero number fixed, because Cargo treats a 0.x minor bump as breaking. "0.8.5" = ≥0.8.5 <0.9.0 and "0.0.3" = exactly the 0.0.3 line (<0.0.4).

Does Cargo support || or hyphen ranges?

No. Cargo rejects || and 1.2 - 2.3. Combine comparisons with a comma instead: ">=1.2, <1.5".

Related

Sources