Email me

Rust errors / Types and traits

Rust compiler error · checked with rustc 1.99.0

Rust error E0271: type mismatch resolving `<i8 as Trait>::AssociatedType == u32`

What it meansA type mismatched an associated type of a trait.

What rustc prints

error[E0271]: type mismatch resolving `<i8 as Trait>::AssociatedType == u32`
  --> main.rs:31:5
   |
31 | foo(3_i8);
   | --- ^^^^ type mismatch resolving `<i8 as Trait>::AssociatedType == u32`
   | |
   | required by a bound introduced by this call
   |
note: expected this to be `u32`
  --> main.rs:21:43
   |
21 | impl Trait for i8 { type AssociatedType = &'static str; }
   |                                           ^^^^^^^^^^^^
note: required by a bound in `foo`
  --> main.rs:5:32
   |
 5 | fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
   |                                ^^^^^^^^^^^^^^^^^^ required by this bound in `foo`

Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.

E0271Types and traits
Activein rustc 1.99.0
8related errors linked below

The compiler team's explanation

A type mismatched an associated type of a trait.

Erroneous code example:

trait Trait { type AssociatedType; }

fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
//                    ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
//                        |            |
//         This says `foo` can         |
//           only be used with         |
//              some type that         |
//         implements `Trait`.         |
//                                     |
//                             This says not only must
//                             `T` be an impl of `Trait`
//                             but also that the impl
//                             must assign the type `u32`
//                             to the associated type.
    println!("in foo");
}

impl Trait for i8 { type AssociatedType = &'static str; }
//~~~~~~~~~~~~~~~   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//      |                             |
// `i8` does have                     |
// implementation                     |
// of `Trait`...                      |
//                     ... but it is an implementation
//                     that assigns `&'static str` to
//                     the associated type.

foo(3_i8);
// Here, we invoke `foo` with an `i8`, which does not satisfy
// the constraint `<i8 as Trait>::AssociatedType=u32`, and
// therefore the type-checker complains with this error code.

The issue can be resolved by changing the associated type: 1) in the foo implementation:

trait Trait { type AssociatedType; }

fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
    println!("in foo");
}

impl Trait for i8 { type AssociatedType = &'static str; }

foo(3_i8);

2) in the Trait implementation for i8:

trait Trait { type AssociatedType; }

fn foo<T>(t: T) where T: Trait<AssociatedType = u32> {
    println!("in foo");
}

impl Trait for i8 { type AssociatedType = u32; }

foo(3_i8);

From rustc --explain E0271 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.

Related errors

All 518 Rust error codes · Rust crate data · Glossary

Up next

Often hit nextE0308: mismatched typesA related error from the same family.Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ