Can I call a Box<FnOnce()>?

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”.

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(|| ());
f();

5 years later, the trait FnBox isn't in the stdlib (unstable or not).
How do we do this these days?

Since Rust 1.35, Box<FnOnce(...)> implements FnOnce, so now it just works.

1 Like

Ahh, I guess my problem is that I'm actually wrapping an FnOnce() in an an object with a consuming method.. okay, i'll just do FnOnce.

Thanks @Riateche

Please don't revive years olds threads, you can create a new thread and link to old ones if you need the context.