Sub topic 5
Version requirements and SemVer compatibility
This chapter assumes you can run cargo new, you have read a [dependencies] table in Cargo.toml, and you know how cargo add writes an entry there. Here you learn what the version string in an entry means, how Cargo uses it to choose a version, and where the rules cause surprises.
SemVer in one page
Cargo uses Semantic Versioning (SemVer). A version has the form MAJOR.MINOR.PATCH, for example 1.4.2. The numbers carry a promise:
- A patch bump (
1.4.2to1.4.3) contains bug fixes only. - A minor bump (
1.4.2to1.5.0) adds features and keeps existing public items working. - A major bump (
1.4.2to2.0.0) may break existing code.
Versions can also have a pre-release suffix (1.5.0-beta.1) and build metadata (1.5.0+abc). Pre-releases sort before the release they precede. Build metadata is ignored when versions are compared.
SemVer is a convention that crate authors follow. Cargo does not check it. If an author ships a breaking change in a minor release, Cargo still treats the release as compatible, and your build can break after an update. Tools such as cargo-semver-checks help authors catch this before publishing, but nothing enforces it.
A requirement is a range
The string in your manifest is a version requirement. It describes a range of versions that you accept, not one version. The version Cargo actually builds with is chosen from that range, as the next chapter on Cargo.lock explains.
# 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.0The sections below cover each operator.
Caret requirements (the default)
A bare version such as "1.2.3" is a caret requirement, the same as writing "^1.2.3". It allows any version that does not change the leftmost non-zero component. In practice that means "everything SemVer says is compatible".
| Requirement | Allowed range |
|---|---|
| `^1.2.3` | `>=1.2.3, <2.0.0` |
| `^1.2` | `>=1.2.0, <2.0.0` |
| `^1` | `>=1.0.0, <2.0.0` |
| `^0.2.3` | `>=0.2.3, <0.3.0` |
| `^0.2` | `>=0.2.0, <0.3.0` |
| `^0.0.3` | `>=0.0.3, <0.0.4` |
| `^0.0` | `>=0.0.0, <0.1.0` |
| `^0` | `>=0.0.0, <1.0.0` |
Before version 1.0.0, SemVer promises nothing, so Cargo uses a stricter convention. For 0.x versions, the minor number acts as the compatibility boundary. For 0.0.x versions, every patch release counts as incompatible. This means foo = "0.2.3" will not pick up 0.3.0, even though a 1.x crate would pick up 1.3.0 without trouble.
You may see cargo add write a short requirement such as "1" or a full one such as "1.0.228". Both are caret requirements on the same major version. They differ only in the lower bound. A full version tells Cargo "I need at least this release". A short one says "any 1.x will do". If your code uses an API added in 1.0.200, write the full version so that nobody resolves an older one.
Tilde requirements
A tilde requirement is narrower. It allows patch updates when you give a minor version, and minor updates when you give only a major version.
| Requirement | Allowed range |
|---|---|
| `~1.2.3` | `>=1.2.3, <1.3.0` |
| `~1.2` | `>=1.2.0, <1.3.0` |
| `~1` | `>=1.0.0, <2.0.0` |
Use tilde when a crate's minor releases have bitten you before, or when you depend on behavior that you only trust within one minor series.
Wildcard, comparison and exact requirements
A wildcard leaves a position open:
1.*means>=1.0.0, <2.0.0.1.2.*means>=1.2.0, <1.3.0.- A bare
*allows any version. crates.io rejects a published crate whose dependency is just*.
Comparison requirements use >, >=, <, <= and =:
[dependencies]
a = ">=1.2"
b = ">1"
c = "<2"
d = "=1.2.3"You can combine several comparisons with commas. A version must satisfy all of them:
[dependencies]
e = ">=1.2, <1.5"An exact requirement such as =1.2.3 pins one version in the manifest. Use it sparingly. Two crates in your tree that both pin the same dependency to different versions in the same compatible range can fail to resolve, and the error message is not friendly. Library authors especially should avoid exact pins. Cargo.lock is the right tool for reproducible builds in your own application.
Pre-release versions
A requirement does not match a pre-release unless the requirement itself names one on the same MAJOR.MINOR.PATCH. ^1.2.3 will never select 1.3.0-alpha.1, even though that version sorts between 1.2.3 and 1.3.0. To try a pre-release, ask for it explicitly:
[dependencies]
some_crate = "1.3.0-alpha.1"This keeps beta releases from leaking into ordinary builds.
Checking requirements in code
You can test these rules with the semver crate, which implements the same matching Cargo uses. Create a project and add the crate:
cargo new semver_play
cd semver_play
cargo add semverThen replace src/main.rs:
use semver::{Version, VersionReq};
fn main() {
let cases = [
("1.2.3", "1.9.0"),
("1.2.3", "2.0.0"),
("0.2.3", "0.2.9"),
("0.2.3", "0.3.0"),
("0.0.3", "0.0.4"),
("~1.2.3", "1.2.9"),
("~1.2.3", "1.3.0"),
("=1.2.3", "1.2.4"),
("1.*", "1.9.9"),
(">=1.2, <1.5", "1.4.0"),
(">=1.2, <1.5", "1.5.0"),
("1.2.3", "1.3.0-alpha.1"),
];
for (req, ver) in cases {
let r = VersionReq::parse(req).unwrap();
let v = Version::parse(ver).unwrap();
println!("{req:<12} {ver:<14} {}", r.matches(&v));
}
}cargo run prints one line per case. The expected results are:
| Requirement | Version | Matches |
|---|---|---|
| `1.2.3` | `1.9.0` | true |
| `1.2.3` | `2.0.0` | false |
| `0.2.3` | `0.2.9` | true |
| `0.2.3` | `0.3.0` | false |
| `0.0.3` | `0.0.4` | false |
| `~1.2.3` | `1.2.9` | true |
| `~1.2.3` | `1.3.0` | false |
| `=1.2.3` | `1.2.4` | false |
| `1.*` | `1.9.9` | true |
| `>=1.2, <1.5` | `1.4.0` | true |
| `>=1.2, <1.5` | `1.5.0` | false |
| `1.2.3` | `1.3.0-alpha.1` | false |
Note that VersionReq::parse("1.2.3") treats a bare version as a caret requirement. That matches Cargo's behavior.
How a candidate version is judged
The flow below shows how Cargo decides whether a caret requirement accepts a candidate version.
How Cargo picks a version
When Cargo resolves your dependency graph, it gathers every requirement placed on a crate from your manifest and from the manifests of your other dependencies. It then picks the highest version that satisfies all of them, within each compatible range.
Two points follow from this.
First, the same crate can appear more than once in a build. If one dependency needs rand = "0.7" and another needs rand = "0.8", Cargo builds both. Their ranges (<0.8.0 and <0.9.0) cannot overlap, so they count as different crates. If two requirements fall in the same range, such as ^1.2 and ^1.4, Cargo unifies them to a single version, typically the latest 1.x.
Second, types from the two copies are not interchangeable. If a function in crate A returns a rand 0.7 type and your code passes it to crate B expecting a rand 0.8 type, the compiler reports a mismatch that looks like "expected Foo, found Foo". Run cargo tree -d to list duplicated crates when you see this kind of error.
Choosing a requirement
| Situation | Suggested requirement |
|---|---|
| Typical dependency of an application | The default caret, with the full version you tested |
| Library you publish | Caret, with the lowest version your code really needs |
| Pre-1.0 crate that changes often | Caret on the `0.x` minor, and review each minor update |
| A crate whose minor releases have broken you | Tilde, for example `~1.2.3` |
| Need a bug fix from a specific patch | Caret with that patch as the lower bound |
| Temporary workaround while testing | Exact `=` pin, with a comment saying why |
A lower bound that is too low is a quiet bug. Your lockfile may resolve to a new release and hide the problem, while a downstream user whose tree has an older version fails to compile. The nightly -Z minimal-versions flag exists to test this. It makes Cargo choose the lowest allowed versions.
Common errors
- "failed to select a version for the requirement": no published version fits. Check the spelling and the range, and remember that pre-releases are excluded.
- "two packages link to the same native library": crates with a
linkskey cannot appear twice, so incompatible ranges of such a crate conflict. Align the requirements. - A
0.xcrate does not update:0.3.1will not move to0.4.0under a caret requirement. Change the requirement by hand, or runcargo add [email protected]. - Unexpected old version: another dependency may demand a narrower range.
cargo tree -i crateshows who requires it.
Exercise
Write the range allowed by foo = "0.4.2", and by bar = "~2.1". Does foo = "0.4.2" accept 0.5.0? Does bar = "~2.1" accept 2.2.0?
Show answer
foo = "0.4.2" is ^0.4.2, which means >=0.4.2, <0.5.0. bar = "~2.1" means >=2.1.0, <2.2.0. Neither 0.5.0 nor 2.2.0 is accepted: for a 0.x version, the minor number is the compatibility boundary, and tilde with a minor version allows only patch updates.
Exercise
Your crate depends on a = "1.3", and another dependency of yours requires a = "^1.5". A third requires a = "2". How many copies of a does Cargo build, and which 1.x version does it choose?
Show answer
Two copies. The ^1.3 and ^1.5 requirements share the range <2.0.0, so Cargo unifies them to one version, the highest 1.x that satisfies both, which is at least 1.5.0. The 2 requirement is in a separate compatible range, so Cargo builds a 2.x as a second copy.
Exercise
Why does ^1.2.3 not match 1.3.0-alpha.1, and what would you write to allow it?
Show answer
Requirements skip pre-release versions unless the requirement itself names a pre-release. To allow it, write a requirement that names one, for example "1.3.0-alpha.1".