expression
Cap bytes read from an async source
Take restricts how many bytes the adapter exposes. Reading limit plus one can detect an oversized complete document, rather than silently accepting a truncated prefix. A byte cap is not a time limit. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): true false Common mistake: Accepting a truncated prefix as the complete document.
Formal Syntax & Structure
rust syntaxrust
reader.take(limit).read_to_end(&mut buf).awaitWorking Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { use tokio::io::AsyncReadExt; let limit = 4usize; for input in [&b"abc"[..], &b"abcdef"[..]] { let mut bytes = Vec::new(); input.take((limit + 1) as u64).read_to_end(&mut bytes).await.unwrap(); println!("{}", bytes.len() <= limit); }}