tokio-rs / website

Website for the Tokio project
https://tokio.rs
MIT License
227 stars 328 forks source link

The shared-state page first example can't build pass. #743

Closed swcumt closed 5 months ago

swcumt commented 6 months ago

With std:: sync::Mutex which not impl Send can't move. The code in this section is

use tokio::net::TcpListener;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

#[tokio::main]
async fn main() {
    let listener = TcpListener::bind("127.0.0.1:6379").await.unwrap();

    println!("Listening");

    let db = Arc::new(Mutex::new(HashMap::new()));

    loop {
        let (socket, _) = listener.accept().await.unwrap();
        // Clone the handle to the hash map.
        let db = db.clone();

        println!("Accepted");
        tokio::spawn(async move {
            process(socket, db).await;
        });
    }
}

The tokio::spawn with error

Darksonn commented 5 months ago

Closing for the reasons outlined in the PR.

There isn't anything fundamentally wrong with the code. Mutex is send.