Consider following code
async fn main_call(&self) -> Result<()>
{
let _list = self.sub_function(/*what to pass here?*/).await;
// do something with the result
Ok(())
}
async fn sub_function<P, F>(&self, f : Option<F>) -> Result<Vec<P>>
where
F : FnOnce(P) -> bool,
P : Clone
{
// do something, and return a value
// the `Vec::new()` is just here for demonstration purposes
Ok(Vec::new())
}
What shall I pass as value to sub_function
, if I don't want to pass a closure? If I pass None
, the compiler complains:
type inside `async fn` body must be known in this context E0698 cannot infer type for type parameter `T`
declared on the enum `Option` Note: the type is part of the `async fn` body because of this `await`
The complaint is obvious, but I'm not sure what I shall pass. None::<FnOnce(12)->bool)
does not work.
expected a `std::ops::FnOnce<(&_,)>` closure, found `dyn std::ops::FnOnce(u32) -> bool`
E0277 expected an `FnOnce<(&_,)>` closure, found `dyn std::ops::FnOnce(u32) -> bool`
Help: the trait `std::ops::FnOnce<(&_,)>` is not implemented for `dyn std::ops::FnOnce(u32) -> bool`
Again, the error message is obvious.