expression

Read a byte chunk asynchronously

The extension method returns the number of bytes actually read. A short read is valid; for a nonempty buffer, zero normally indicates end of input. Work with only the initialized prefix containing new data. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 3 abc Common mistake: Printing the entire buffer as if every byte came from input.

Formal Syntax & Structure

rust syntaxrust
AsyncReadExt::read(&mut buf).await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use tokio::io::AsyncReadExt;    let mut input = &b"abc"[..];    let mut buf = [0; 8];    let n = input.read(&mut buf).await.unwrap();    println!("{} {}", n, String::from_utf8_lossy(&buf[..n]));}
Rust Reference