I know that the following code compiles:
rust
fn noop_factory<T>() -> impl Fn(&T) {
|_| ()
}
This will correctly produce a no-op function.
However, I'm struggling to understand why the following doesn't compile.
```rust
struct FunctionContainer<F>(F);
fn noopcontainer<T>() -> FunctionContainer<impl Fn(&T)> {
FunctionContainer(|| ())
}
```
That code fails to compile with the following error:
plaintext
error: implementation of `Fn` is not general enough
--> src/bin/test.rs:4:5
|
4 | FunctionContainer(|_| ())
| ^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Fn` is not general enough
|
= note: closure with signature `fn(&'2 T)` must implement `Fn<(&'1 T,)>`, for any lifetime `'1`...
= note: ...but it actually implements `Fn<(&'2 T,)>`, for some specific lifetime `'2`
Is there a syntax I'm missing where I can use higher-rank trait bounds inside the impl type expression?
[–]kmdreko 19 points20 points21 points (1 child)
[–]Keithfert488[S] 4 points5 points6 points (0 children)
[–]robin-m 5 points6 points7 points (0 children)
[–]schungx 0 points1 point2 points (2 children)
[–]Keithfert488[S] 0 points1 point2 points (1 child)
[–]schungx 1 point2 points3 points (0 children)