Can't the compiler copy a static reference?

fn f(a: &'static ()) -> impl Fn() {
    || g(a)
}

fn g(_: &'static ()) {}

The compiler requires me to write move here.
It's simple, but a little weird. Why can't the compiler copy a static reference?

It's even stranger when you realize that || g(&*a) also works

1 Like

It's a side effect of how closure captures work. Those prefer to capture by reference, and can capture Copy types by reference even when you need them by value.

I think the difference with &*a is that it captures *a by reference instead.

1 Like