Closed
Description
This code (playground)
trait MyTrait {
fn my_fn();
}
struct MyStruct;
impl MyTrait for MyStruct {
fn my_fn() {}
}
fn main() {
MyTrait::my_fn();
}
currently produces this error:
error[E0283]: type annotations needed
--> src/main.rs:12:5
|
2 | fn my_fn();
| ----------- required by `MyTrait::my_fn`
...
12 | MyTrait::my_fn();
| ^^^^^^^^^^^^^^ cannot infer type
|
= note: cannot satisfy `_: MyTrait`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0283`.
When I recently stumbled across this error, it took me a little while to figure out what the problem was.
I was trying to call the trait function directly and not an implementation of the function.
I think this may be even more confusing for beginners.
I think the following error message would have helped me (and may help others) understand what the problem is and how to fix it much more quickly:
error[E0283]: a concrete implementation is needed to call the trait function
--> src/main.rs:12:5
|
12 | MyTrait::my_fn();
| ^^^^^^^^^^^^^^ cannot infer concrete implementation of `my_fn`
|
= note: cannot satisfy `_: MyTrait`
= help: try calling `my_fn` on an implementation of `MyTrait` such as `MyStruct::my_fn`
= help: available implementations are `MyStruct`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0283`.
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: Type inferenceArea: Suggestions generated by the compiler applied by `cargo fix`Category: An issue proposing an enhancement or a PR with one.Diagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint; hard to understand for new users.Relevant to the compiler team, which will review and decide on the PR/issue.