expression

Convert a standard TCP listener

Before registering a standard socket with Tokio, configure it nonblocking and enter a runtime with I/O enabled. Otherwise the socket can block a runtime thread or violate the API requirements. Tokio is an external crate. Authorized isolated IPv4 loopback fixture; no public network or DNS. Expected stdout (not executed): true Common mistake: Converting a blocking standard listener without setting nonblocking mode.

Formal Syntax & Structure

rust syntaxrust
TcpListener::from_std(listener)

Working Example

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