expression
Read a complete fixed-size field
Use a fixed-size buffer when the format requires a known byte count. Incomplete input is an error. Cancellation can leave the underlying stream partly consumed; do not retry from the beginning of a partially consumed field. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): true Common mistake: Assuming read_exact can be cancelled and restarted without retaining progress.
Formal Syntax & Structure
rust syntaxrust
reader.read_exact(&mut buf).awaitWorking Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { use tokio::io::AsyncReadExt; let mut input = &b"ab"[..]; let mut buf = [0; 3]; let e = input.read_exact(&mut buf).await.unwrap_err(); println!("{}", e.kind() == std::io::ErrorKind::UnexpectedEof);}