expression

Drain stream bytes before handling hangup

A hangup notification does not imply the receive buffer is empty. A stream may still have queued bytes after its peer closes. This local Unix socket example demonstrates the read behavior, not an actual poll call. Expected stdout (not executed): data: ok EOF: true Common mistake: Discarding queued data as soon as a hangup is reported. Run only in the isolated fixture runner, not a public code playground.

Formal Syntax & Structure

rust syntaxrust
POLLIN / POLLHUP; read until EOF or WouldBlock

Working Example

Example Snippetrust
use std::{io::{self, Read, Write}, os::unix::net::UnixStream};fn main() -> io::Result<()> {    let (mut writer, mut reader) = UnixStream::pair()?;    writer.write_all(b"ok")?;    drop(writer);    let mut bytes = [0; 2];    reader.read_exact(&mut bytes)?;    println!("data: {}", std::str::from_utf8(&bytes).unwrap());    println!("EOF: {}", reader.read(&mut [0; 1])? == 0);    Ok(())}
Rust Reference