I’m still struggling with Rust generics. The code below simplifies the problem I’m currently facing as much as possible. This code results in a compilation error, and I simply can’t understand why it isn’t working. The details have been documented within the source code comments. Could you please take a look and offer any advice?
// Assume this trait is NOT object safe, so we cannot use dynamic dispatch
// like `Box<dyn Callable>` and `&dyn Callable`.
trait Callable {
fn call(&self);
}
struct Test;
impl Callable for Test {
fn call(&self) {}
}
fn f<T: Callable>() {
let callback = |v: T| { v.call(); };
// The `Test` type has implemented the `Callable` trait. Then why can't
// `Test{}` be passed to a variable as `T: Callable`?
callback(Test{});
}
fn main() {
f::<Test>();
}