For the following code,
trait SomeTrait {
type T;
fn hello(a: Self::T);
}
struct A;
impl SomeTrait for A {
type T = i32;
fn hello(a: i32) {
println!("{}", a);
}
}
impl A {
fn foo(&self) {
let a: i32 = 10;
SomeTrait::hello(a);
}
}
fn main() {
let a = A;
a.foo();
}
I got compile error
error[E0283]: type annotations needed
--> main.rs:23:9
|
23 | SomeTrait::hello(10);
| ^^^^^^^^^^^^^^^^ cannot infer type
|
= note: cannot satisfy `_: SomeTrait`
Why cannot the Rust compiler infer that the call to SomeTrait::hello()
is the one defined for struct A
? To my understanding, there is no ambiguity here because there is only one hello()
method that takes an i32
parameter.
I know this problem can be solved by changing the call to Self::hello(a)
or <A as SomeTrait>::hello(a)
. But I saw some crate that has code pattern like above. So I'd like to know why the compile fails here.