Can I call a Box<FnOnce()>?


#1

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

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

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


#2

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


#3

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

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