1

I'm wondering, how to match by a macro? Something like this:

macro_rules! call_this_closure {
    ($closure:???) => {
        let x = 0;
        $closure(x);
    };
}

fn main() {

    call_this_closure!(|x|println!("{}", x));
}

I found Creating environment for closure in a macro in Rust but it uses the trick to transform the macro in a tt so it cannot have arguments: (|| 5 + x). In my case, I want the macro to have arguments.

I know it can be solved with a generic function like this:

pub fn call_this_closure<F, A, R>(&mut self, f: F) -> R
    where F: Fn(&mut A) -> R {
    //...
}

but I actually need a closure in my problem.

Gatonito
  • 1,662
  • 5
  • 26
  • 55

0 Answers0