Hello!
I've got an issue of calling a function passing a closure. I get the program compiled if I pass closure as rvalue (line 27). Something goes wrong if I pass a closure as a variable (line 29).
As far as I got the compilation error I should define lifetime for the closure arguments. But I don't know why the compiler starts care about it. I can lock environment using rvalue of a closure as well as lvalue.
Ok. I tried to define a lifetime for the closure. Code here. Compiler says that heap
argument doesn't outlive borrowed content in recursive calling. I pointed the lifetime of the heap
covers parent
time and I don't understand what it should cover else. There is no other options except the closure argument.
How can I help the compiler to understand that heap
lives long enough? Why does it work for a closure passed as rvalue?
One more tip. I can get the code compiled if I pass function instead of closure (line 29). I though that the compiler infers function type instead of closure type on line 27 and I lock an outside variable. Look
let ref a = 1;
make_tree(&mut Node::new(123), &mut vec!(1,2,3), &|x, _| x == a);
It compiles as well. So I see only one difference. The compiler can't infer lifetimes only if I pass lvalue of a closure.