Can I call a Box<FnOnce()>?


Can I call a Box<FnOnce()>? If I write

let f: Box<FnOnce()> = Box::new(|| ());

I get “cannot move a value of type core::ops::FnOnce(): the size of core::ops::FnOnce() cannot be statically determined”.


A temporary workaround is to use Invoke. It’s not clear how this is going to be resolved for 1.0.


FnOnce now extends a new std::boxed::FnBox trait (#23939) that works like this:

let f: Box<FnBox()> = Box::new(|| ());