Reference to function doesn't live long enough


I have the following code (Rust playground):

struct FnObjectCreator<'a, T: 'a>(&'a Fn() -> T);

impl<'a, T: 'a + Default> FnObjectCreator<'a, T> {
    fn new_default() -> FnObjectCreator<'a, T> {

I was hoping that taking the reference &T::default would be fine, but unfortunately the compiler treats it as a temporary function-local variable, and complains that it doesn’t live long enough. Am I missing something about the type system that the compiler doesn’t treat T::default as 'static?


Maybe you’re looking for plain fn pointer? That is, fn() -> T. Otherwise, you’re trying to create a temp closure value (which internally calls T::default) and returning a reference to it.


Oh that works perfectly; thanks!