Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0050: method `foo` has 1 parameter but the declaration in trait `Foo::foo` has 2
What it meansAn attempted implementation of a trait method has the wrong number of function parameters.
What rustc prints
error[E0050]: method `foo` has 1 parameter but the declaration in trait `Foo::foo` has 2
--> main.rs:10:12
|
2 | fn foo(&self, x: u8) -> bool;
| ------------ trait requires 2 parameters
...
10 | fn foo(&self) -> bool { true }
| ^^^^^ expected 2 parameters, found 1Real 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
An attempted implementation of a trait method has the wrong number of function parameters.
Erroneous code example:
trait Foo {
fn foo(&self, x: u8) -> bool;
}
struct Bar;
// error: method `foo` has 1 parameter but the declaration in trait `Foo::foo`
// has 2
impl Foo for Bar {
fn foo(&self) -> bool { true }
}
For example, the Foo trait has a method foo with two function parameters (&self and u8), but the implementation of foo for the type Bar omits the u8 parameter. To fix this error, they must have the same parameters:
trait Foo {
fn foo(&self, x: u8) -> bool;
}
struct Bar;
impl Foo for Bar {
fn foo(&self, x: u8) -> bool { // ok!
true
}
}From rustc --explain E0050 (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
- E0046not all trait items implemented, missing: `foo`
- E0049associated function `foo` has 0 type parameters but its trait declaration has 1 type parameter
- E0053method `foo` has an incompatible type for trait
- E0055reached the recursion limit while auto-dereferencing `Foo`
Up next
Often hit nextE0308: mismatched typesA related error from the same family.Read it next →Did this answer your question?