Version Requirements & SemVer
When you write serde = "1.0.200" you are not asking for version 1.0.200. You are writing a version requirement: a rule that says which versions are acceptable. Cargo's resolver then picks the newest acceptable version that exists. Misreading that rule is the source of most "but I pinned it!" surprises.
Semantic Versioning in one paragraph
Crates follow Semantic Versioning: MAJOR.MINOR.PATCH. A breaking change bumps MAJOR, a compatible feature bumps MINOR, a fix bumps PATCH. Cargo adds one rule of its own for 0.x versions: the left-most non-zero component is treated as the major one. So 0.2.0 → 0.3.0 is breaking, and so is 0.0.3 → 0.0.4.
The operators
| Requirement | Means | Matches |
|---|---|---|
"1.2.3" or "^1.2.3" |
caret (the default): compatible with 1.2.3 | >=1.2.3, <2.0.0 |
"1.2" |
caret with missing parts = 0 | >=1.2.0, <2.0.0 |
"1" |
caret | >=1.0.0, <2.0.0 |
"0.2.3" |
caret on a 0.x version | >=0.2.3, <0.3.0 |
"0.0.3" |
caret on 0.0.x | >=0.0.3, <0.0.4 |
"~1.2.3" |
tilde: patch updates only | >=1.2.3, <1.3.0 |
"~1" |
tilde with only a major | >=1.0.0, <2.0.0 |
"=1.2.3" |
exactly this version | 1.2.3 |
">=1.2, <1.5" |
comparison operators, comma = AND | 1.2.0 … 1.4.x |
"1.*", "*" |
wildcards | any 1.x / anything |
The important consequence: a bare version is a minimum, not a pin. "1.2" happily resolves to 1.9.4.
Watching the resolver pick
The private registry in our sessions makes this easy to see. The session publishes seven versions of a tiny crate units, each one reporting its own version, then builds an app against different requirements:
crate-registry start
cargo new --lib units --vcs none -q
cd units
sed -i 's/^edition.*/&\ndescription = "Unit helpers"\nlicense = "MIT"\ndocumentation = "https:\/\/example.com"/' Cargo.toml
for v in 0.1.0 0.1.1 0.2.0 1.0.0 1.2.0 1.3.0 2.0.0; do
sed -i "s/^version = .*/version = \"$v\"/" Cargo.toml
echo "pub const VERSION: &str = \"$v\";" > src/lib.rs
cargo publish --registry local --no-verify -q
done
cd ..
cargo new app --vcs none -q
cd app
echo 'fn main() { println!("got units {}", units::VERSION); }' > src/main.rs
for req in '1' '1.2' '~1.2' '=1.2.0' '0.1' '>=1.0, <1.3' '*' '3'; do
sed -i '/^units/d' Cargo.toml
echo "units = { version = \"$req\", registry = \"local\" }" >> Cargo.toml
rm -f Cargo.lock
printf '%-14s ' "\"$req\""
cargo run -q 2>&1 | head -2
done"1" got units 1.3.0
"1.2" got units 1.3.0
"~1.2" got units 1.2.0
"=1.2.0" got units 1.2.0
"0.1" got units 0.1.1
">=1.0, <1.3" got units 1.2.0
"*" got units 2.0.0
"3" error: failed to select a version for the requirement `units = "^3"`
candidate versions found which didn't match: 2.0.0, 1.3.0, 1.2.0, ...
Every line follows the table: "1" and "1.2" both take the newest 1.x (1.3.0), ~1.2 stays on 1.2.x, "0.1" never crosses into 0.2, and the comma requirement takes the newest version below 1.3. "*" takes 2.0.0, a breaking upgrade nobody reviewed, which is why crates.io refuses to publish crates with wildcard dependencies. Asking for "3" fails with the list of versions that do exist.
Note: the session deletes
Cargo.lockbefore each build. With a lockfile present, Cargo keeps the version it already locked even if the requirement would allow a newer one. That is the subject of the next lesson.
A requirement matcher, as code
The rules are mechanical enough to implement in a few lines. This model expands each requirement into a range and picks the newest match, reproducing the session above:
def parse(v):
parts = [int(x) for x in v.split(".")]
return parts + [0] * (3 - len(parts)), len(parts) # padded version, how many parts were given
def caret(v):
(ma, mi, pa), n = parse(v)
lo = (ma, mi, pa)
if ma > 0 or n == 1: hi = (ma + 1, 0, 0)
elif mi > 0 or n == 2: hi = (0, mi + 1, 0)
else: hi = (0, 0, pa + 1)
return lo, hi
def tilde(v):
(ma, mi, pa), n = parse(v)
return (ma, mi, pa), ((ma + 1, 0, 0) if n == 1 else (ma, mi + 1, 0))
def matches(req, version):
ver = tuple(parse(version)[0])
for part in req.split(","):
c = part.strip()
if c == "*": continue
for op in (">=", "<=", ">", "<", "=", "~", "^"):
if c.startswith(op):
c = c[len(op):].strip()
break
else:
op = "^" # a bare version means caret
v = tuple(parse(c)[0])
ok = {">=": ver >= v, "<=": ver <= v, ">": ver > v, "<": ver < v, "=": ver == v}.get(op)
if ok is None:
lo, hi = caret(c) if op == "^" else tilde(c)
ok = lo <= ver < hi
if not ok:
return False
return True
published = ["0.1.0", "0.1.1", "0.2.0", "1.0.0", "1.2.0", "1.3.0", "2.0.0"]
for req in ["1", "1.2", "~1.2", "=1.2.0", "0.1", ">=1.0, <1.3", "*", "3", "0.0.3"]:
ok = [v for v in published if matches(req, v)]
best = max(ok, key=lambda v: parse(v)[0]) if ok else "no match"
print(f"{req!r:14} -> {best:9} candidates: {', '.join(ok) or '-'}")'1' -> 1.3.0 candidates: 1.0.0, 1.2.0, 1.3.0
'1.2' -> 1.3.0 candidates: 1.2.0, 1.3.0
'~1.2' -> 1.2.0 candidates: 1.2.0
'=1.2.0' -> 1.2.0 candidates: 1.2.0
'0.1' -> 0.1.1 candidates: 0.1.0, 0.1.1
'>=1.0, <1.3' -> 1.2.0 candidates: 1.0.0, 1.2.0
'*' -> 2.0.0 candidates: 0.1.0, 0.1.1, 0.2.0, 1.0.0, 1.2.0, 1.3.0, 2.0.0
'3' -> no match candidates: -
'0.0.3' -> no match candidates: -
Real Cargo also handles pre-releases (1.0.0-beta.2 only matches requirements that mention a pre-release of the same version) and build metadata, and it unifies requirements from all dependents, which this model doesn't. But the core idea is exactly this.
Choosing requirements
- Default to caret with the version you tested:
serde = "1.0.200". It allows bug fixes and new features, never breaking changes, and documents the minimum you need. Writing"1"claims your code works with 1.0.0, which is rarely tested. - Libraries keep requirements wide (caret), so many crates can share one version of a dependency.
- Applications rely on
Cargo.lockfor exact versions, not on=requirements.=in a library makes it impossible for its users to resolve to anything else and causes conflicts. - Never
*. crates.io rejects it, and it's a time bomb in private registries too.
Test yourself
Versions 1.2.0, 1.3.0 and 2.0.0 exist. Which does serde = "1.2" resolve to without a lockfile?
- 1.2.0
- 1.3.0
- 2.0.0
- It fails because 1.2 is not a full version
What does "0.2.3" allow?
- >=0.2.3, <1.0.0
- >=0.2.3, <0.3.0
- only 0.2.3
- >=0.2.3, <0.2.4
You need bug-fix updates only, staying on 1.4.x. Which requirement?
- "1.4"
- "~1.4"
- "=1.4"
- "^1.4"