I don’t understand why spawn_i is FnOnce, and not FnMut, since the argument is mutable.

The argument is not relevant, closure classifications is based on what they close over and how.

Here, the issue is that spawn_i consumes f when called:

  • when spawn_i is created, f is moved into it
  • when spawn_i is called the first time, it creates the sub-closure and f is moved into that
  • the second time spawn_i would be called… it can’t be because there’s no f to move into the sub-closure anymore

therefore spawn_i can only be called once.

So you need f to be reusable somehow e.g. store it in an Arc, or make it a function pointer (fn(usize) -> T) instead of a trait bound, or maybe play around with lifetimes.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top