std-method
Nonblocking TCP accept
Switch a fresh listener to nonblocking mode. With no pending connection, accept reports WouldBlock. An event-driven program must wait for readiness or schedule a bounded retry rather than spin. Expected stdout (not executed): true Common mistake: Using a tight unbounded loop around nonblocking accept.
Formal Syntax & Structure
rust syntaxrust
listener.set_nonblocking(true); listener.accept()Working Example
Example Snippetrust
fn main() { use std::net::{Ipv4Addr, TcpListener}; let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); listener.set_nonblocking(true).unwrap(); let e = listener.accept().unwrap_err(); println!("{}", e.kind() == std::io::ErrorKind::WouldBlock);}