Why is the parameter type of the closure required to be explicitly specified?

⚓ rust    📅 2025-05-06    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 59 days ago. The information described in this article may have changed.

Consider this example:

trait MyFn<T> {}
impl<T, F, R> MyFn<T> for F where F: Fn(T) -> R {}
fn call<F>(_: F)
where
    F: for<'a> MyFn<&'a str>,
{
}
fn main() {
    call(|s /*:&str*/| async {});
}

The compiler complains that:

error: implementation of `FnOnce` is not general enough
 --> src/main.rs:9:5
  |
9 |     call(|s /*:&str*/| async {});
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'2 str) -> {async block@src/main.rs:9:24: 9:29}` must implement `FnOnce<(&'1 str,)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`

However, if the comment is uncommented, the code will be compiled. In contrast, if directly using the Fn trait, the explicitly specified parameter type is not necessary.

fn call<F, U>(_: F)
where
    F: for<'a> FnOnce(&'a str) -> U,
    U: Future,
{
}
fn main() {
    call(|s /*:&str*/| async {});
}

This code is Ok. However, if assigning the closure to a variable and passing the variable as the argument, will get a similar error as above. What's the reason here?

1 post - 1 participant

Read full topic

🏷️ rust_feed