Open
Description
trait MyTrait<T> {
type Output;
}
impl<T> MyTrait<T> for T {
type Output = T;
}
fn show<T: MyTrait<T, Output = T> + Default>() -> T::Output {
T::default()
}
fn main() {
let i: i32 = show();
}
In this case, the compiler cannot infer the type for T
, however, as specified in the trait bound T: MyTrait<T, Output = T>
, which implies that T::Output
is just T
. If we change T::Output
to T
, then the compiler will infer T
as i32
.
In contrast, the compiler can infer Fn::Output
, for example:
fn infer_output<F,U:Default>(_:F)->F::Output
where F:Fn()->U
{
U::default()
}
fn function<U:Default>()->U{
U::default()
}
fn main() {
let i:i32 = infer_output(function);
}
In this example, the compiler can know that F::Output
is U
, and infer U
with type i32
. The compiler may enhance type inference to make it more powerful for the first example.