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

Why async move block is not needed? #162

Open
mkbanchi opened this issue Sep 27, 2024 · 0 comments
Open

Why async move block is not needed? #162

mkbanchi opened this issue Sep 27, 2024 · 0 comments

Comments

@mkbanchi
Copy link

mkbanchi commented Sep 27, 2024

Looking at the very interesting crawler implementation in ch-05, i'm struggling with the fact that async block at crawler.rs:137 does not need to be move. queued_url is String that is not Copy so, even if it is cloneed inside the closure passed to for_each_concurrent, this should not suffice, it is borrowed inside the async block. I reprodued the problem with a similar (I think) example:

use futures::stream::StreamExt;
use tokio::sync::mpsc::{channel, Receiver, Sender};
use tokio_stream::wrappers::ReceiverStream;

#[tokio::main]
async fn main() {
    let v: Vec<String> = vec!["1".into(), "2".into(), "3".into()];

    let (tx, rx): (Sender<String>, Receiver<String>) = channel(3);

    let f = tokio::spawn(async move {
        ReceiverStream::new(rx)
            .for_each_concurrent(3, |val| {
                let val = val.clone();
                async {  // <-- This needs to be move
                    println!("Processed value: {}", val.clone());
                }
            })
            .await;
    });

    for i in v.iter() {
        tx.send(i.clone()).await.unwrap();
    }

    drop(tx);

    f.await.unwrap();

    println!("Finished");
}

The above code does not compile because of the missing move async block highlighted by the comment in the above snippet.

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

1 participant