I am trying to write flexible code in rust and have come to this problem just now. I want to have a function that applies some user-defined behavior to the data. But because the parameter f of function alter actually has a type of Box<(dyn FnOnce(...) + 'static)>, told by the compiler, I cannot directly use the reference num since it is not static. I then tried to copy it in inside the box to make it live as long as the box. But the error is still there. I am wondering why this is the case and is there any workaround to this?
Thank you so much. I tried to catch the essence of the original problem, so I simplify the type a bit. It seems I have failed to do so. I have updated code now, it's closer to the original problem. Those functions were originally inside a trait and I want it to be object-safe so I used a Box there. The move keyword doesn't help now, what's the difference, why it worked previously but not now?
Thank you again! The code type checks now! I certainly don't want the type parameter to be static as it will make a lot of other things static as well and make the code not so elegant or efficient I suppose. But I think the more interesting part now is why we have to manually set the lifetime and why move does not work this time, are there any readings or blogs that are relevant and you can point me to? Appreciate it.