expression

Distinguish a nonblocking wait from EOF

A blocking read can wait for data. A nonblocking read returns promptly when no data is ready. WouldBlock is not EOF: retry after readiness notification, not in an unbounded busy loop. With a one-byte buffer and a locally controlled writer, the second read can succeed after data is sent. Expected stdout (not executed): waiting then Q Common mistake: Treating WouldBlock as zero bytes or spinning continuously. Run only in the isolated fixture runner, not a public code playground.

Formal Syntax & Structure

rust syntaxrust
stream.set_nonblocking(true); ErrorKind::WouldBlock

Working Example

Example Snippetrust
use std::{io::{self,Read,Write,ErrorKind}, os::unix::net::UnixStream};fn main() -> io::Result<()> {    let (mut a,mut b)=UnixStream::pair()?;    b.set_nonblocking(true)?;    let mut byte=[0;1];    let err=b.read(&mut byte).unwrap_err();    assert_eq!(err.kind(), ErrorKind::WouldBlock);    a.write_all(b"Q")?;    b.read_exact(&mut byte)?;    println!("waiting then {}",char::from(byte[0])); Ok(())}
Rust Reference