Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0119: conflicting implementations of trait for type
What it meansTwo impl blocks could both apply to the same type, so Rust cannot pick one. Often a blanket impl (impl<T: X> Tr for T) overlaps a specific one.
What rustc prints
error[E0119]: conflicting implementations of trait `MyTrait` for type `Foo`
--> main.rs:13:1
|
5 | impl<T> MyTrait for T {
| --------------------- first implementation here
...
13 | impl MyTrait for Foo { // error: conflicting implementations of trait
| ^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `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.
Why it happens
- A blanket impl plus a specific impl for a type that also meets the blanket's bounds.
- A derive (like
#[derive(Clone)]) plus a manual impl of the same trait. - Your
impl From<X> for Yclashing with the standard library'simpl<T> From<T> for T.
How to fix it
1. Remove one of the two
Drop the derive or the manual impl.
2. Narrow the blanket impl
Use a marker trait or a newtype so the two never overlap.
struct Meters(f64);
impl From<Meters> for f64 { fn from(m: Meters) -> f64 { m.0 } }Other wordings of this error
conflicting implementations of trait for type, conflicting implementation in crate, first implementation here
The compiler team's explanation
There are conflicting trait implementations for the same type.
Erroneous code example:
trait MyTrait {
fn get(&self) -> usize;
}
impl<T> MyTrait for T {
fn get(&self) -> usize { 0 }
}
struct Foo {
value: usize
}
impl MyTrait for Foo { // error: conflicting implementations of trait
// `MyTrait` for type `Foo`
fn get(&self) -> usize { self.value }
}
When looking for the implementation for the trait, the compiler finds both the impl<T> MyTrait for T where T is all types and the impl MyTrait for Foo. Since a trait cannot be implemented multiple times, this is an error. So, when you write:
trait MyTrait {
fn get(&self) -> usize;
}
impl<T> MyTrait for T {
fn get(&self) -> usize { 0 }
}
This makes the trait implemented on all types in the scope. So if you try to implement it on another one after that, the implementations will conflict. Example:
trait MyTrait {
fn get(&self) -> usize;
}
impl<T> MyTrait for T {
fn get(&self) -> usize { 0 }
}
struct Foo;
fn main() {
let f = Foo;
f.get(); // the trait is implemented so we can use it
}From rustc --explain E0119 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0308mismatched types
- E0277the trait bound is not satisfied
- E0599no method named X found in the current scope
- E0433failed to resolve
- E0117only traits defined in the current crate can be implemented for types defined outside of the crate
- E0118no nominal type found for inherent implementation
- E0120the `Drop` trait may only be implemented for local structs, enums, and unions
- E0121the placeholder `_` is not allowed within types on item signatures for return types
Up next
Often hit nextE0308: mismatched typesA related error from the same family.Read it next →Did this answer your question?