I have the following code:
struct X { i: i32 }
trait MyTrait<T> {
fn hello(&self);
}
impl MyTrait<i32> for X {
fn hello(&self) { println!("I'm an i32") }
}
impl MyTrait<String> for X {
fn hello(&self) { println!("I'm a String") }
}
fn main() {
let f = X { i: 0 };
f.hello();
}
which obviously does not compile as the compiler cannot disambiguate to which trait the f.hello()
belongs. So I'm getting the error
error[E0282]: type annotations needed
--> src\main.rs:17:7
|
17 | f.hello();
| ^^^^^ cannot infer type for type parameter `T` declared on the trait `MyTrait`
Is there any way to annotate the type of hello
so I can tell the compiler to e.g. invoke MyTrait<String>::hello
on f
?