Why can't compiler infer a type here ?
Compiler refuses to compile this:
use std::ops::Mul;
struct Stuff{
a: i128
}
impl Mul<i64> for Stuff {
type Output = Stuff;
fn mul (self, other: i64) -> Stuff {
Stuff{a: self.a * (other as i128)}
}
}
impl Mul<i32> for Stuff {
type Output = Stuff;
fn mul (self, other: i32) -> Stuff {
Stuff{a: self.a * (other as i128)}
}
}
fn main() {
let stuff = Stuff{a: 42};
let y = stuff * 2;
println!("{:?}", y.a);
}
Compiler says:
error[E0282]: type annotations needed
--> src/main.rs:25:9
|
25 | let y = stuff * 2;
| ^
26 |
27 | println!("{:?}", y.a);
| - type must be known at this point
|
help: consider giving `y` an explicit type
|
25 | let y: /* Type */ = stuff * 2;
| ++++++++++++
For more information about this error, try `rustc --explain E0282`.
y is obviously a Stuff, why can't the compiler see that ? Note that:
- compiler manages to compile if I only have one
Mulfunction - compiler manages to compile if I specify
y's type.