async-rs / async-std

Async version of the Rust standard library

Home Page:https://async.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

expected `()`, found `JoinHandle<()>`

zonglin-hub opened this issue · comments

use async_std::net::TcpListener;
use async_std::net::TcpStream;
use async_std::task::spawn;
use futures::stream::StreamExt;

#[tokio::main]
async fn main() {
    let tcp_listener = TcpListener::bind("127.0.0.1:7878").await.unwrap();
    tcp_listener
        .incoming()
        .for_each_concurrent(None, |tcpstream| async move {
            let tcpstream = tcpstream.unwrap();
            spawn(handle_connection(tcpstream))
        })
        .await;
}

async fn handle_connection(_tcpstream: TcpStream) -> Result<(), std::io::Error> {
    // ...
    Ok(())
}
error[E0271]: expected `[async block@src\main.rs:11:48: 14:10]` to be a future that resolves to `()`, but it resolves to `JoinHandle<Result<(), Error>>`
    --> src\main.rs:11:10
     |
11   |         .for_each_concurrent(None, |tcpstream| async move {
     |          ^^^^^^^^^^^^^^^^^^^ expected `()`, found `JoinHandle<Result<(), Error>>`
     |
     = note: expected unit type `()`
                   found struct `async_std::task::JoinHandle<Result<(), std::io::Error>>`
note: required by a bound in `for_each_concurrent`
    --> C:\Users\liuzonglin\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\futures-util-0.3.28\src\stream\stream\mod.rs:1148:21
     |
1141 |     fn for_each_concurrent<Fut, F>(
     |        ------------------- required by a bound in this associated function
...
1148 |         Fut: Future<Output = ()>,
     |                     ^^^^^^^^^^^ required by this bound in `StreamExt::for_each_concurrent`

error[E0271]: expected `[async block@src\main.rs:11:48: 14:10]` to be a future that resolves to `()`, but it resolves to `JoinHandle<Result<(), Error>>`
  --> src\main.rs:15:10
   |
15 |         .await;
   |          ^^^^^ expected `JoinHandle<Result<(), Error>>`, found `()`
   |
   = note: expected struct `async_std::task::JoinHandle<Result<(), std::io::Error>>`
           found unit type `()`
   = note: required for `ForEachConcurrent<async_std::net::Incoming<'_>, [async block@src\main.rs:11:48: 14:10], [closure@src\main.rs:11:36: 11:47]>` to implement `futures::Future`
   = note: required for `ForEachConcurrent<async_std::net::Incoming<'_>, [async block@src\main.rs:11:48: 14:10], [closure@src\main.rs:11:36: 11:47]>` to implement `std::future::IntoFuture`

modify

spawn(handle_connection(tcpstream));

Can run normally,

My understanding is whether to add ";" or not, why is there a problem with this place?