Cannot infer type when passing None to Option<impl FnMut()>

Viewed 522

I have something like this:

fn my_func(cb: Option<impl FnMut(i32) + 'static>) { ... }

If I try to pass in None, such as: my_func(None), I get:

cannot infer type for type parameter `impl FnMut(i32) + 'static` declared on the method `my_func`

I found a previous post that said I still need to specify a type via None::<T> using something that implements T, and that makes sense, but how do I do that in this case? I tried defining an empty function that matches the signature, but that didn't work. Rust complained I wasn't passing in any "types", and I was passing too many "consts".

1 Answers

You probably tried to pass in an actual function instead of the type of a function. You can use None::<fn(i32)>, since fn(i32) is a concrete type that implements the FnMut(i32) + 'static trait bound.

Link to playground

Related