工作
Rust 的工作系統是採用輕量執行緒的形式。
一項工作具有一個頂層 Future,執行器會輪詢該 Future 來推動進度。該 Future 可能有一或多個巢狀 Future,供其 poll
方法輪詢,可約略對應至呼叫堆疊。藉由輪詢多個子項 Future (例如競爭的計時器和 I/O 作業),即可在工作內實現並行機制。
use tokio::io::{self, AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpListener; #[tokio::main] async fn main() -> io::Result<()> { let listener = TcpListener::bind("127.0.0.1:0").await?; println!("listening on port {}", listener.local_addr()?.port()); loop { let (mut socket, addr) = listener.accept().await?; println!("connection from {addr:?}"); tokio::spawn(async move { socket.write_all(b"Who are you?\n").await.expect("socket error"); let mut buf = vec![0; 1024]; let name_size = socket.read(&mut buf).await.expect("socket error"); let name = std::str::from_utf8(&buf[..name_size]).unwrap().trim(); let reply = format!("Thanks for dialing in, {name}!\n"); socket.write_all(reply.as_bytes()).await.expect("socket error"); }); } }