expression

In-memory bidirectional async pipe

Create paired byte endpoints for local testing. Each endpoint can receive what the other sends. Keep writes within capacity or let the reader run concurrently to prevent a stalled test. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): ping Common mistake: Awaiting an oversized write before starting the reader.

Formal Syntax & Structure

rust syntaxrust
tokio::io::duplex(capacity)

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use tokio::io::{AsyncReadExt, AsyncWriteExt};    let (mut a, mut b) = tokio::io::duplex(4);    a.write_all(b"ping").await.unwrap();    let mut buf = [0; 4];    b.read_exact(&mut buf).await.unwrap();    println!("{}", String::from_utf8_lossy(&buf));}
Rust Reference