skip to content

In Cargo, crates require `toml = "1.1"`, `"1.0"` and `"0.9"`; how many toml copies get built, and which command shows why?

level: middleimportance: must knowfreq 58%

answer

  1. caret ranges, not exact versions
  2. one pick per compatible range
  3. 0.x: the minor is the boundary
  4. duplicates view inverts the tree

basics

~20 s

Two copies. "1.1" and "1.0" share the compatible range >=1.0.0, <2.0.0, so Cargo unifies them on one 1.x version; "0.9" means >=0.9.0, <0.10.0, a separate range. cargo tree -d lists every crate built in several versions and who pulls each.

solid answer

~40 s

Cargo's requirements are caret ranges, and the resolver keeps **one version per SemVer-compatible range**. `"1.1"` and `"1.0"` both sit in `>=1.0.0, <2.0.0`, so they unify on the highest version that satisfies both (1.1.8 here). For a `0.x` version the minor number is the compatibility boundary, so `"0.9"` is `>=0.9.0, <0.10.0` and gets its own copy. `cargo tree -d` (`--duplicates`) prints only packages present in more than one version, inverted so each shows the chain that pulls it in; `cargo tree -i [email protected]` narrows to one copy. If two requirements in the same range cannot share a version, such as `=0.4.11` and `=0.4.8`, Cargo reports an error instead of building both.

code

console · 15 lines
console
$ cargo tree -d -e normal
toml v0.9.12+spec-1.1.0
└── legacy-config v0.1.0 (/…/legacy-config)
    └── gateway v0.1.0 (/…/gateway)

toml v1.1.8+spec-1.1.0
├── gateway v0.1.0 (/…/gateway)
└── rules v0.1.0 (/…/rules)
    └── gateway v0.1.0 (/…/gateway)

$ cargo tree -i toml
error: specification `toml` is ambiguous
help: re-run this command with one of the following specifications
  [email protected]+spec-1.1.0
  [email protected]+spec-1.1.0

go deeper

for a junior

Remember that "1.0" in Cargo.toml is a range, and that one crate name can end up compiled more than once.

for a middle

Explain one version per SemVer-compatible range, the 0.x minor boundary, and how cargo tree -d and -i lead you to the crate that pulls an old version.

for a senior

Tell a harmless duplicate (extra build time) from a harmful one (types crossing a version boundary) and know when Cargo errors instead of duplicating.

for a principal

Decide how much duplication a team tolerates and whether CI should report or block on new duplicate crates.

## The rule Cargo applies Cargo assumes every crate follows **SemVer** and reads a bare requirement such as `"1.1"` as a **caret requirement**: the lowest acceptable version plus everything up to the next incompatible release. The resolver then tries to reuse one version wherever it can, because one copy builds faster and lets types from that crate move between the libraries that use it. The unit of reuse is the **compatible range**: | Requirement | Meaning | Compatible range | |---|---|---| | `"1.1"` | `>=1.1.0, <2.0.0` | 1.x | | `"1.0"` | `>=1.0.0, <2.0.0` | 1.x | | `"0.9"` | `>=0.9.0, <0.10.0` | 0.9.x only | For versions `0.y.z` with `y` above zero, Cargo treats the **minor** number as the breaking part, so `0.9` and `0.10` are as incompatible as `1.x` and `2.x`. (What each operator allows in detail belongs to the material on dependency specifications.) Applied to the question: 1. `"1.1"` and `"1.0"` fall in the same 1.x range. Cargo picks **one** version that satisfies both, the highest available at lock time, here 1.1.8. 2. `"0.9"` cannot be met by any 1.x release, so Cargo adds a **second** package, 0.9.12. 3. The build compiles two `toml` crates, and each one's own dependencies may split too: here `toml_datetime` and `winnow` also appear twice. ## Seeing it with cargo tree `cargo tree` prints the resolved graph. Three options matter for duplicates: - `-d` / `--duplicates` shows only packages that appear in more than one version. It **implies `--invert`**, so each duplicate is printed at the top with the chain of crates that depend on it underneath: you read straight down to the dependency that pins the old version. - `-i SPEC` / `--invert SPEC` shows who depends on one package. When two versions exist a bare name is rejected as ambiguous, and Cargo lists the full specs to use, such as `[email protected]+spec-1.1.0`; a shorter unambiguous prefix like `[email protected]` also works. - `-e KINDS` / `--edges KINDS` filters edge kinds. The default is `normal,build,dev`; `-e normal` hides dev-dependencies when you care only about what ships. For a full picture across platforms add `--target all`, since `cargo tree` otherwise shows only the host's target-specific dependencies. When nothing is duplicated, `cargo tree -d` prints `warning: nothing to print.` ## When Cargo errors instead Building two copies is only allowed **across** compatible ranges. Inside one range Cargo insists on a single version; if the requirements leave no common version, it backtracks and finally **fails resolution** rather than building two. The Cargo book's example is one package requiring `log = "=0.4.11"` and another `log = "=0.4.8"`: both are 0.4.x, so Cargo will not pick two, and no single version meets both. A narrow tilde requirement like `"~1.3"` beside `"1.4"` fails for the same reason. The `links` key adds a related constraint: two packages that link the same native library cannot coexist even in different ranges, so the resolver errors there too. ## Reading the output - `(*)` after a package means its subtree was already printed elsewhere; `--no-dedupe` repeats it. - A suffix such as `+spec-1.1.0` is SemVer **build metadata** that the crate put in its version; a requirement such as `"1"` still matches it. - Path dependencies show their directory in parentheses, registry crates show only the version. - With `-d`, the indentation reads upwards: the package at the left margin is the duplicate, each indented line is a crate that depends on the line above it. ## Why duplicates matter - **Build time and binary size**: each copy is compiled and linked separately. - **Type identity**: a `toml::Table` from 0.9 and one from 1.1 are different types to the compiler, so passing one where the other is expected fails to compile. - **Accidental splits**: an open requirement like `">=0.6"` can make the resolver pick the newest release for one crate even when an older one would have satisfied everyone; the Cargo book notes the resolver does not deduplicate that case, and `cargo update --precise` can merge them by hand. The usual workflow: run `cargo tree -d`, follow each old copy down to the crate that asks for it, then check whether a newer release of that crate has moved to the current major.

  • Why doesn't changing your own requirement from toml = "1.1" to "1.0" remove the 0.9 copy?
    Both of those are in the 1.x compatible range, so they only constrain the 1.x copy. The 0.9 copy exists because another crate asks for `"0.9"`, and nothing in your requirement can satisfy it. Removing a duplicate means changing what the crate pulling the old version asks for, usually by upgrading it to a release on the newer major.
  • One crate requires log = "=0.4.11" and another log = "=0.4.8". What does Cargo do?
    It fails resolution. Both versions are in the 0.4.x compatible range, where Cargo allows only one version, and no single version satisfies both exact requirements. Cargo backtracks looking for other choices and, finding none, reports a conflict naming both requirements instead of building two copies.

  • Every distinct requirement string gets its own copy
  • Cargo always unifies on one version per crate name
  • 0.9 and 0.10 are compatible because only the minor changed
  • cargo tree -i works with a bare name even when versions differ
  • Cargo builds two copies when exact pins in one range conflict