Back to listRust
Tokio Async Runtime
Lv.5715@mukitaro8 playsDec 28, 2025
Tokio async TCP echo server. Rust's most popular async runtime.
preview.rust
1use tokio::net::TcpListener;2use tokio::io::{AsyncReadExt, AsyncWriteExt};3 4#[tokio::main]5async fn main() -> Result<(), Box<dyn std::error::Error>> {6 let listener = TcpListener::bind("127.0.0.1:8080").await?;7 8 loop {9 let (mut socket, _) = listener.accept().await?;10 11 tokio::spawn(async move {12 let mut buf = [0; 1024];13 14 loop {15 let n = match socket.read(&mut buf).await {16 Ok(n) if n == 0 => return,17 Ok(n) => n,18 Err(_) => return,19 };20 21 if socket.write_all(&buf[0..n]).await.is_err() {22 return;23 }24 }25 });26 }27}Custom problems are not included in rankings