expression

Borrowed TCP halves

Borrow the read and write directions independently for concurrent I/O. Both halves remain tied to the original stream lifetime, so they cannot freely escape to independently owned tasks. Tokio is an external crate. Authorized isolated IPv4 loopback fixture; no public network or DNS. Expected stdout (not executed): go Common mistake: Trying to move a borrowed half into a static spawned task.

Formal Syntax & Structure

rust syntaxrust
stream.split()

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    tokio::time::timeout(std::time::Duration::from_secs(3), async {        use tokio::net::{TcpListener, TcpStream};        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();        let addr = listener.local_addr().unwrap();        let (client, accepted) = tokio::join!(TcpStream::connect(addr), listener.accept());        let mut client = client.unwrap();        let (mut server, _) = accepted.unwrap();        use tokio::io::{AsyncReadExt, AsyncWriteExt};        let (mut rd, mut wr) = client.split();        wr.write_all(b"go").await.unwrap();        let mut buf = [0; 2];        server.read_exact(&mut buf).await.unwrap();        server.write_all(&buf).await.unwrap();        rd.read_exact(&mut buf).await.unwrap();        println!("{}", std::str::from_utf8(&buf).unwrap());    }).await.expect("fixture deadline");}
Rust Reference