#![warn(rust_2018_idioms)] use std::error::Error; use std::net::SocketAddr; use std::{env, io}; use tokio::net::UdpSocket; struct Server { socket: UdpSocket, buf: Vec, to_send: Option<(usize, SocketAddr)>, } impl Server { async fn run(self) -> Result<(), io::Error> { let Server { socket, mut buf, mut to_send, } = self; loop { if let Some((size, peer)) = to_send { let amt = socket.send_to(&buf[..size], &peer).await?; println!("Echoed {}/{} bytes to {}", amt, size, peer); } to_send = Some(socket.recv_from(&mut buf).await?); } } } #[tokio::main] async fn main() -> Result<(), Box> { let addr = env::args() .nth(1) .unwrap_or_else(|| "127.0.0.1:3000".to_string()); let socket = UdpSocket::bind(&addr).await?; println!("Listening on {}", socket.local_addr()?); let server = Server { socket, buf: vec![0; 1024], to_send: None, }; server.run().await?; Ok(()) }