Pls explain executing flow

async fn accept_loop(addr: impl ToSocketAddrs) -> Result<()> {
    let listener = TcpListener::bind(addr).await?;
    let mut incoming = listener.incoming();
    while let Some(stream) = incoming.next().await {
        let stream = stream?;
        println!("Accepting from: {}", stream.peer_addr()?);
        let _handle = task::spawn(connection_loop(stream)); // 1
    }
    Ok(())
}

What happening at this line.
Is thread waiting to complete or
Move immidiatly to next line

let _handle = task::spawn(connection_loop(stream))```

When you spawn a task it'll run the task "in the background" and give you some sort of handle which can be used to retrieve the result at some point later on.

Is it like thread?
Means spawn the task in background and folow immediately to next line to execution ?

It is similar to a thread, yes.

And it is like golang go keyword right

Yes.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.