expression

Transform binary data in fixed-size chunks

Read only the bytes actually returned by Read. This simple ASCII uppercasing transformation is binary-safe for non-ASCII bytes, not Unicode case conversion. A finite cursor stands in for a file or pipe. Fixture: Expected stdout (not executed): RUST LAB Expected stderr: (empty) Expected exit: 0 Common mistake: Writing the whole buffer after a short final read, leaking stale bytes.

Formal Syntax & Structure

rust syntaxrust
loop { let n = input.read(&mut buf)?; ... }

Working Example

Example Snippetrust
fn main() -> std::io::Result<()> {    use std::io::{Cursor, Read, Write};    let mut input = Cursor::new(b"rust lab");    let mut buf = [0u8; 3];    let mut output = std::io::stdout().lock();    loop {        let n = input.read(&mut buf)?;        if n == 0 { break; }        buf[..n].make_ascii_uppercase();        output.write_all(&buf[..n])?;    }    output.write_all(b"\n")?;    output.flush()?;    Ok(())}
Rust Reference