Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't move variable into closure in special case #86709

Closed
FH0 opened this issue Jun 29, 2021 · 2 comments
Closed

can't move variable into closure in special case #86709

FH0 opened this issue Jun 29, 2021 · 2 comments

Comments

@FH0
Copy link

FH0 commented Jun 29, 2021

use futures::Future;
use tokio::sync::mpsc::channel;

fn foo<F, S>(mut f: F)
where
    F: FnMut() -> S,
    S: Future,
{
    f();
}

#[tokio::main]
async fn main() {
    let (tx, mut rx) = channel::<bool>(1);
    foo(move || async move {
        let _ = rx.recv().await;
    });
}

I want to move rx into closure but failed.
image

I checked a lot of information but still can't solve it.

@fee1-dead
Copy link
Member

You expect the closure to be FnMut, but it can't. This is because you can't call it multiple times, since calling it moves rx into an async block.

@FH0
Copy link
Author

FH0 commented Jun 29, 2021

I did some attempts and it can only be called once. Finally I solved the problem using macros.

@FH0 FH0 closed this as completed Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants