expression

Track partial-write progress

Keep a byte offset across successful writes. A nonempty remaining slice with a zero-byte write cannot make progress; report WriteZero. This pattern preserves progress when the enclosing operation retains the offset. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 5 Common mistake: Restarting a partly written record from offset zero.

Formal Syntax & Structure

rust syntaxrust
writer.write(&bytes[offset..]).await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use tokio::io::AsyncWriteExt;    let data = b"hello";    let mut offset = 0;    let mut out = Vec::new();    while offset < data.len() {        let n = out.write(&data[offset..]).await.unwrap();        assert!(n > 0, "write made no progress");        offset += n;    }    println!("{offset}");}
Rust Reference