Sub topic 5
Version requirements and SemVer compatibility
This chapter assumes you can create a project with cargo new, read a Cargo.toml file, and run commands in a terminal. It explains what the version string next to a dependency means and how Cargo uses it to choose a version.
Semantic versioning in brief
Crates on crates.io use SemVer (semantic versioning). A version has three numbers: MAJOR.MINOR.PATCH, for example 1.4.2.
- A patch release (
1.4.2to1.4.3) fixes bugs and keeps the public API the same. - A minor release (
1.4.2to1.5.0) adds features and stays backward compatible. - A major release (
1.4.2to2.0.0) may break existing code.
Cargo relies on this promise. When you ask for 1.4.2, Cargo may give you any later version that should still compile with your code.
The default is a caret requirement
A version string with no operator is a caret requirement. serde = "1" means ^1, and Cargo may pick any version that is compatible with 1.x.y. The rule is that the leftmost non-zero component is the compatibility boundary.
# Each requirement and the range it allows
serde = "1.2.3" # ^1.2.3 : >=1.2.3, <2.0.0
foo = "0.2.3" # ^0.2.3 : >=0.2.3, <0.3.0
bar = "0.0.3" # ^0.0.3 : >=0.0.3, <0.0.4
baz = "~1.2.3" # >=1.2.3, <1.3.0
qux = "=1.2.3" # exactly 1.2.3
quux = "1.*" # >=1.0.0, <2.0.0Look at the 0.x cases. SemVer treats versions below 1.0.0 as unstable, so Cargo treats a minor bump in 0.2.3 to 0.3.0 as breaking. A 0.0.x version allows no upgrades at all.
Other operators
| Requirement | Meaning | When to use it |
|---|---|---|
| `1.2.3` or `^1.2.3` | Compatible updates | Almost always |
| `~1.2.3` | Patch updates only | You want to avoid new minor versions |
| `=1.2.3` | Exactly this version | A known bug in other versions |
| `1.*` | Any 1.x.y | Rarely needed |
| `>=1.2, <1.5` | An explicit range | Combining a lower and upper bound |
Pinning with = is a last resort. If two of your dependencies each pin a different exact version of a shared crate, Cargo cannot satisfy both and the build fails.
How Cargo chooses a version
A requirement describes a range, not a single version. Cargo picks the highest version in the range that also fits every other crate in the graph.
The same crate can appear in the tree at two different major versions, such as rand 0.7 and rand 0.8. Cargo treats these as separate crates. Within one compatible range, Cargo uses only one copy.
Writing requirements in practice
Start with the version cargo add writes for you. It records the current release, so the lower bound is a version you have actually built against.
cargo add serde --features deriveIf you use an API added in 1.0.150, raise the lower bound to 1.0.150. The requirement then states the oldest version your code works with. A lower bound that is too low can let a resolver pick a version that lacks the API.
Avoid a bare * for a crate you publish. crates.io rejects wildcard-only requirements for dependencies, because they promise nothing about compatibility.
Common errors
- "failed to select a version for the requirement": no published version fits. Check for a typo, such as
serd, or a requirement like99. - Version conflict between crates: two dependencies need incompatible ranges of a shared crate. Loosen your own pins first, then check which crates ask for which version (the
cargo treechapter covers this). - A
0.xupgrade that never arrives:foo = "0.2"will not move to0.3. Edit the requirement yourself.
Exercise
What range does foo = "0.4.1" allow, and does it allow 0.5.0? What about bar = "~2.3.0"?
Show answer
foo = "0.4.1" means ^0.4.1, which is >=0.4.1, <0.5.0, so 0.5.0 is not allowed. bar = "~2.3.0" means >=2.3.0, <2.4.0, so only patch updates are allowed.
Exercise
Why is it risky to pin two dependencies of your project to = versions?
Show answer
If they depend on a shared crate with incompatible exact versions, Cargo cannot find one version that satisfies both, and resolution fails. Exact pins also block security and bug-fix updates.