expression

Async TCP listener binding

Create a listening socket. Port zero requests an available local port from the OS. Use local_addr to discover it; binding does not itself accept connections. Tokio is an external crate. Authorized isolated IPv4 loopback fixture; no public network or DNS. Expected stdout (not executed): true true Common mistake: Binding to all interfaces for a local exercise.

Formal Syntax & Structure

rust syntaxrust
TcpListener::bind(addr).await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    tokio::time::timeout(std::time::Duration::from_secs(3), async {        let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();        let addr = l.local_addr().unwrap();        println!("{} {}", addr.ip().is_loopback(), addr.port() != 0);    }).await.expect("fixture deadline");}
Rust Reference