trait

Read: input contract

A reader fills some of the supplied buffer and reports the valid byte count. Process only that prefix. For a nonempty buffer, zero can signal EOF, though some readers may later yield more data. Expected stdout (not executed): 2 ok Common mistake: Processing all eight bytes after a two-byte read.

Formal Syntax & Structure

rust syntaxrust
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>

Working Example

Example Snippetrust
fn main() {    use std::io::Read;    let mut source = &b"ok"[..];    let mut buf = [0u8; 8];    let n = source.read(&mut buf).unwrap();    println!("{} {}", n, std::str::from_utf8(&buf[..n]).unwrap());}
Rust Reference