I almost have an intuitive sense of why this code should NOT work, but I can't quite put my finger on it. I think it has something to do with the fact that the new function would have a different return type every time.
Why is that a problem? Why does the direct creation work?
struct Struct<T>
where
T: Fn(&[u8]),
{
func: T,
}
impl<T> Struct<T>
where
T: Fn(&[u8]),
{
fn new() -> Struct<T> {
// this doesn't work
Struct { func: |msg| {} }
}
}
fn main() {
// this works
let s = Struct { func: |msg| {} };
}
The error is
error[E0308]: mismatched types
--> src/main.rs:14:24
|
14 | Struct { func: |msg| {} }
| ^^^^^^^^ expected type parameter, found closure
|
= note: expected type `T`
found type `[closure@src/main.rs:14:24: 14:32]`