expression

Poll a local listener for an incoming connection

A nonblocking accept returns WouldBlock when no connection is queued. A readiness-driven loop waits for another opportunity rather than polling continuously. This example queues exactly one connection from the same process, then accepts it. The accepted stream mode should be configured explicitly rather than assumed inherited. Expected stdout (not executed): accepted one connection Common mistake: Assuming accepted sockets inherit nonblocking mode on every OS. Run only in the isolated fixture runner, not a public code playground.

Formal Syntax & Structure

rust syntaxrust
UnixListener::set_nonblocking(true); listener.accept()

Working Example

Example Snippetrust
use std::{fs,io::{self,ErrorKind},os::unix::net::{UnixListener,UnixStream}};fn main() -> io::Result<()> {    let listener=UnixListener::bind("accept.sock")?;    listener.set_nonblocking(true)?;    assert_eq!(listener.accept().unwrap_err().kind(), ErrorKind::WouldBlock);    let client=UnixStream::connect("accept.sock")?;    let (accepted,_)=listener.accept()?;    accepted.set_nonblocking(false)?;    drop(accepted); drop(client); drop(listener); fs::remove_file("accept.sock")?;    println!("accepted one connection"); Ok(())}
Rust Reference