Want to move self in async FnMut

Hey guys, I'm learning rust and trying my first project in rust.

I do want to know if there has any (design) pattern that can solve this problem.
Or, is the mspc the only way to solve it? I haven't tried it.
I've checked The Book, skimmed async rust, and checked rustc --explain E0507, it seems not solved my problem.
It's nice to have some help, thanks a lot.

Please prefer to use code blocks instead of images:

// your code

It'd also be nice to post the full error message (also in a code block).

Since handleCommands is not an async function (there's no await), it is not possible to run it concurrently. You can run it non-concurrently like this:

use futures::stream::StreamExt; // for next()

let mut stream = rx.commands::<Command, &str>("");
while let Some((cx, command, _)) = stream.next() {
    self.handle_commands(cx, command);

I can't see the signature of handle_commands, but I'm guessing the error you got is because handle_commands takes &mut self, which requires exclusive access, which also prevents you from running it concurrently. If it took &self, it would probably have compiled, although it would not run concurrently even with for_each_concurrent due to the missing await.

FnMut can be called multiple times, but things can be moved one time only. If Rust allowed this to compile, the second call to FnMut would crash, because it'd try to move something that's already gone.

You can work around such things either with .clone() or if you expect it to be called only once despite FnMut, then Option.take().

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.