expression

Bound reads from ordinary files

Ordinary file I/O may use blocking work behind Tokio’s interface. Do not assume cancelling the waiting future rolls back filesystem effects. Use a bounded reader for a controlled file and explicitly handle oversize data. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): true Common mistake: Trusting a prior file-size check instead of limiting the actual read.

Formal Syntax & Structure

rust syntaxrust
File::open(path).await; file.take(limit)

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use tokio::io::{AsyncReadExt, AsyncWriteExt};    let path = "bm-v61-cap.txt";    let mut f = tokio::fs::OpenOptions::new().write(true).create_new(true).open(path).await.unwrap();    f.write_all(b"123456").await.unwrap();    f.flush().await.unwrap();    drop(f);    let mut bytes = Vec::new();    tokio::fs::File::open(path).await.unwrap().take(5).read_to_end(&mut bytes).await.unwrap();    println!("{}", bytes.len() > 4);    tokio::fs::remove_file(path).await.unwrap();}
Rust Reference