expression
Readable readiness and try_read
Readiness is a notification to attempt I/O, not proof it will succeed. Retry WouldBlock only after awaiting readiness again; treat other errors and EOF separately. Tokio is an external crate. Authorized isolated IPv4 loopback fixture; no public network or DNS. Expected stdout (not executed): z Common mistake: Busy-looping after WouldBlock.
Formal Syntax & Structure
rust syntaxrust
stream.readable().await; stream.try_read(&mut buf)Working Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { tokio::time::timeout(std::time::Duration::from_secs(3), async { use tokio::net::{TcpListener, TcpStream}; let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); let addr = listener.local_addr().unwrap(); let (client, accepted) = tokio::join!(TcpStream::connect(addr), listener.accept()); let mut client = client.unwrap(); let (mut server, _) = accepted.unwrap(); use tokio::io::AsyncWriteExt; client.write_all(b"z").await.unwrap(); let mut b = [0; 1]; loop { server.readable().await.unwrap(); match server.try_read(&mut b) { Ok(1) => { println!("{}", b[0] as char); break; } Ok(_) => panic!("unexpected EOF"), Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => continue, Err(e) => panic!("{e}"), } } }).await.expect("fixture deadline");}