I'd like to have multiple versions of a function optimized for type of its arguments, and Rust call appropriate one depending on context.
In my case all arguments have the same type, and all are equivalent, so it'd rather avoid having a self
argument.
I've tried this code:
trait Foo<T> {
fn foo(a: T, b: T, c: T);
}
impl Foo<i32> {
fn foo(a: i32, b: i32, c: i32) {}
}
impl Foo<i16> {
fn foo(a: i16, b: i16, c: i16) {}
}
fn main() {
Foo::foo(1i32,2,3);
Foo::foo(1i16,2,3);
}
but Rust requires type annotations:
error: type annotations required: cannot resolve
_ : Foo<i32>
[E0283]
Can I avoid providing type annotations at the call site? If I have to, how to do it?