Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0053: method `foo` has an incompatible type for trait
What it meansThe parameters of any trait method must match between a trait implementation and the trait definition.
What rustc prints
error[E0053]: method `foo` has an incompatible type for trait --> main.rs:10:15 | 10 | fn foo(x: i16) { } | ^^^ expected `u16`, found `i16` | note: type in trait --> main.rs:2:15 | 2 | fn foo(x: u16); | ^^^ = note: expected signature `fn(u16)` found signature `fn(i16)` help: change the parameter type to match the trait | 10 - fn foo(x: i16) { } 10 + fn foo(x: u16) { } |
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.
The compiler team's explanation
The parameters of any trait method must match between a trait implementation and the trait definition.
Erroneous code example:
trait Foo {
fn foo(x: u16);
fn bar(&self);
}
struct Bar;
impl Foo for Bar {
// error, expected u16, found i16
fn foo(x: i16) { }
// error, types differ in mutability
fn bar(&mut self) { }
}From rustc --explain E0053 (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
- E0049associated function `foo` has 0 type parameters but its trait declaration has 1 type parameter
- E0050method `foo` has 1 parameter but the declaration in trait `Foo::foo` has 2
- E0055reached the recursion limit while auto-dereferencing `Foo`
- E0059type parameter to bare `FnOnce` trait must be a tuple
Up next
Often hit nextE0308: mismatched typesA related error from the same family.Read it next →Did this answer your question?