Help with lifetimes and closure

I have stuck with this issue where I pass a reference to function(e.g. fn_1) to another function(fn_2) and call fn_1 inside fn_2 with a variable created inside fn_2. The Issue is fn_1 takes references and hence I have to provide lifetimes. But I can't find way to get past this error.

Below is a small snippet shows what I am trying to do.

Playground Link:

Please! If anyone can help me.


You need higher-ranked trait bounds here, which lets you talk about an arbitrarily-short lifetime:

fn call_fn<'b, F>(v1: &'b Value, mut fn_: F) -> Value
where F: for<'a> FnMut(&'a mut Value, &'b Value) -> &'a Value{
    let mut v2 = Value{v:0};
    let _ = fn_(&mut v2, &v1);

Your original version takes 'a as a parameter from outside. This means it refers to a stack frame outside of call_fn, which prevents any &'a reference pointing to a local variable.

1 Like

That is exactly what I was looking for. Thanks!