I've tried writing a closure that receives a closure (let's call it A) that returns a closure that receives a value and then applies closure A to it.
Sample:
let do_some = |f: &dyn Fn(u32) -> u32| move |x: u32| f(x);
let result = do_some(&|v: u32| v * 1111)(7);
Observation:
- the reason the closure
f
is a&dyn Fn()
is because it is the only way the compiler would let me pass a closure to another closure - the move operator on the innermost closure is to avoid
f
being borrowed by the innermost closure
Problem:
error: lifetime may not live long enough
--> src/main.rs:2:44
|
2 | let do_some = |f: &dyn Fn(u32) -> u32| move |x: u32| f(x);
| - - ^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
| | |
| | return type of closure `[closure@src/main.rs:2:44: 2:57]` contains a lifetime `'2`
| let's call the lifetime of this reference `'1`
How to specify lifetime when closures don't allow generic lifetime specification via thee <'a'>
notation?