skip to content

A Cargo build fails with E0308 "expected `Map<String, Value>`, found a different `Map<String, Value>`" between your code and a dependency; what happened and how do you fix it?

level: seniorimportance: should knowfreq 45%

answer

  1. same name, different crate
  2. two compatible ranges in the graph
  3. note names multiple versions
  4. align the major or convert

basics

~20 s

Two SemVer-incompatible versions of toml are in the graph, and rustc treats each version's types as unrelated, so a toml::Table from 0.9 cannot be passed where your 1.x toml::Table is expected. Align both sides on one major, or convert at the boundary.

solid answer

~50 s

Cargo built two `toml` crates because your code asks for `"1"` and the dependency for `"0.9"`, which are different compatible ranges. To rustc they are separate crates, so `toml::Table` from 0.9 and from 1.x are distinct types even though the path and the name match; E0308 says "found a different" type and adds the note "there are multiple different versions of crate `toml` in the dependency graph". `cargo tree -d` shows which crate pulls the old version. The fixes, best first: upgrade the dependency (or edit it, if it lives in your repository) so both use the same major; pin your own requirement to the dependency's major if you need nothing newer; or convert at the boundary through a stable form such as text or std types. `cargo update` alone never merges two compatible ranges.

code

rust · 8 lines
rust
fn timeout(cfg: &toml::Table) -> i64 {
    cfg["timeout_ms"].as_integer().unwrap_or(1000)
}

fn main() {
    let cfg = legacy_config::defaults();
    println!("{}", timeout(&cfg));
}

go deeper

for a junior

Recognise that a crate name can be compiled twice in one build, and that the copies' types do not mix.

for a middle

Explain why toml 0.9 and 1.x land in different compatible ranges and read the multiple-versions note under E0308.

for a senior

Diagnose with cargo tree -d and -i, rank the fixes (upgrade, align, convert, patch), and say why a duplicate that never crosses an API is harmless.

for a principal

Weigh the cost of public APIs that expose third-party types, and how a team tracks duplicate majors before they surface as errors.

## Why the same type name is not the same type Cargo keeps **one version per SemVer-compatible range** but happily builds several ranges side by side. `toml = "0.9"` (meaning `>=0.9.0, <0.10.0`) and `toml = "1"` (meaning `>=1.0.0, <2.0.0`) can never share a version, so the build compiles two separate `toml` libraries. To the Rust compiler each compiled library is a **distinct crate**, and every type, trait and static belongs to exactly one crate. `toml::Table` from 0.9.12 and `toml::Table` from 1.1.8 have the same source path and maybe identical fields, but they are as unrelated as two types with different names. Nothing converts one into the other automatically, and a trait implemented for one is not implemented for the other. ## Reading the error The failure appears when a value crosses between the two worlds, typically a dependency's public API returning its `toml` type into code written against yours: - `error[E0308]: mismatched types` with "expected `Map<String, Value>`, found a different `Map<String, Value>`". The words **"a different"** with an identical name are the signal. - rustc adds `note: there are multiple different versions of crate `toml` in the dependency graph`, pointing at the type definition in each copy, and a help line suggesting `cargo tree`. Other shapes of the same problem: a trait-bound error where a type implements the trait from one version but the function wants the other, or a `downcast_ref` that quietly returns `None` because the value was created with the other version's type. The Cargo book calls these **version-incompatibility hazards**: usually a compile error, occasionally runtime misbehaviour. ## Diagnosing 1. Run `cargo tree -d` to list crates present in more than one version; each appears above the chain that depends on it. 2. Run `cargo tree -i [email protected]` to see exactly who requires the old copy. 3. Check whether that crate has a newer release on the current major, or whether it exposes the old type in its public API at all. ## When a duplicate is harmless Two copies do not always mean trouble. If each dependent uses its copy **internally** and never exposes it in its public API, the build compiles and runs correctly; the cost is extra compile time and a larger binary. `cargo tree -d` on a large service usually shows several such duplicates, often small utility crates. The ones worth chasing are crates whose **types or traits travel** between libraries: serialisation values, error types, async runtime handles, HTTP types. Those turn into E0308 or trait-bound errors the moment two libraries are wired together. ## Fixing it | Fix | When it fits | |---|---| | Upgrade the dependency to a release using the same major | The usual answer; check its changelog for the bump | | Edit the dependency's requirement | It is a path dependency in your own repository | | Lower your own requirement to its major | You use nothing from the newer major | | Convert at the boundary | You cannot change either side yet | | Patch the dependency | A fork that moves it to the newer major, via `[patch]`, as a stopgap | Converting at the boundary means passing something both sides understand: std types, or a serialised form. For `toml` that can be as simple as `to_string()` on the old table and `parse()` into the new one, at the cost of a round trip. What does **not** work: - Expecting resolver `"3"` to merge them. It only changes which release Cargo prefers by Rust version; one copy per compatible range still applies. - A renamed second dependency such as `toml09 = { package = "toml", version = "0.9" }` compiles, because your code can then name `toml09::Table`, but it keeps both copies and spreads the split into your own code. - Hoping `cargo update` merges them. It never crosses a compatible range boundary on its own. ## Preventing it - Libraries that expose a dependency's types in their public API tie their users to that dependency's major. Re-exporting the dependency (`pub use toml;`) lets users write against the exact version the library uses. - Library authors publishing a new major of a widely used crate can apply the **semver trick**: publish a point release of the old major that re-exports the new major's types, so both versions share them. (Which API changes force a major is covered in the Rust language material on API evolution.) - A team can watch `cargo tree -d` in review for new duplicates, especially of crates whose types travel between libraries.

  • Could the binary depend on both majors at once to accept the old table?
    Yes: a renamed dependency, `toml09 = { package = "toml", version = "0.9" }`, lets the code name `toml09::Table` and accept the value. It compiles, but both copies stay in the build and every function touching that value now names an old major. It is a stopgap; aligning both sides on one major removes the second copy.
  • Can two incompatible versions coexist without causing any error?
    Yes, as long as their types never meet. If each dependent uses its own copy internally and exposes no types from it, the build compiles and only pays extra build time and binary size. The trouble starts when a type, trait or `Any` value from one version reaches code written against the other.

  • Two versions of one crate always fail to build
  • Types with the same path are the same type across versions
  • cargo update will merge 0.9 and 1.x into one copy
  • Setting resolver = "3" merges incompatible copies
  • The compiler converts between versions if the fields match