Open
Description
Code
trait Foo {
fn foo(&self) -> &str;
}
struct Example;
impl Foo for Example {
fn foo() -> &str {
"example"
}
}
Current output
error[E0106]: missing lifetime specifier
--> src/lib.rs:7:17
|
7 | fn foo() -> &str {
| ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
7 | fn foo() -> &'static str {
| +++++++
help: instead, you are more likely to want to return an owned value
|
7 | fn foo() -> String {
| ~~~~~~
error[E0186]: method `foo` has a `&self` declaration in the trait, but not in the impl
--> src/lib.rs:7:5
|
2 | fn foo(&self) -> &str;
| ---------------------- `&self` used in trait
...
7 | fn foo() -> &str {
| ^^^^^^^^^^^^^^^^ expected `&self` in impl
Desired output
error[E0186]: method `foo` has a `&self` declaration in the trait, but not in the impl
--> src/lib.rs:7:5
|
2 | fn foo(&self) -> &str;
| ---------------------- `&self` used in trait
...
7 | fn foo() -> &str {
| ^^^^^^^^^^^^^^^^ expected `&self` in impl
Rationale and extra context
When the function is a trait associated function, the signature must match (or refine) the trait’s, so E0106 and any other errors that would propose changes to the signature are misleading or at least not nearly as relevant as E0186 (or E0050 or E0053 or any other such mismatch). Therefore, they should be hidden.
This is particularly important for situations involving a missing self
parameter, because (in my personal experience) it’s easy to forget to write self
in the parameter list.
Rust Version
1.84.0