Multiple type parameters and closures


#1

Hi,
when trying to compile the following code:

trait Foo {
    fn say_hello(&self);
    fn new() -> Self;
}

struct Bar;

impl Foo for Bar {
    fn say_hello(&self) {
        println!("Hello");
    }

    fn new() -> Bar {
        Bar
    }
}

fn call<T: Foo, F: Fn(T)>(f: F) {
    f(T::new());
}

fn main() {
    call::<Bar>(|p| p.say_hello());
}

I get the error "too few type parameters provided: expected 2 parameters, found 1 parameter", but as far as I understood, the type of the closure can’t be named. What is the proper way of doing this?


#2

in this case

    call::<Bar, _>(|p| p.say_hello());

works.


#3

You can use _ as a stand-in for types you want the compiler to infer: https://is.gd/Ic7GNw


#4

Thank you!