extern crate tokio; use crate::tokio::io::AsyncWriteExt; use std::error::Error; use std::io; use tokio::net::TcpStream; #[tokio::main] async fn main() -> Result<(), Box> { let mut stream = TcpStream::connect("irc.freenode.net:6667").await?; stream.writable().await?; stream.write(b"NICK garrit\r\n").await?; stream.write(b"USER garrit 8 x : garrit").await?; stream.write(b"JOIN #antimony").await?; loop { // Wait for the socket to be readable stream.readable().await?; // Creating the buffer **after** the `await` prevents it from // being stored in the async task. let mut buf = [0; 8]; // Try to read data, this may still fail with `WouldBlock` // if the readiness event is a false positive. match stream.try_read(&mut buf) { Ok(0) => break, Ok(n) => { let text = std::str::from_utf8(&buf)?; print!("{}", text); } Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { continue; } Err(e) => { return Err(e.into()); } } } Ok(()) }