4.1 小型模式集锦

原文链接: https://book.async.rs/patterns/small-patterns.html

一系列简短、实用的模式集合。

拆分流

async-std 并未在 io 句柄上提供 split() 方法。将流拆分为读端和写端可以这样做:

1
2
3
4
5
6
# extern crate async_std;
use async_std::{io, net::TcpStream};
async fn echo(stream: TcpStream) {
    let (reader, writer) = &mut (&stream, &stream);
    io::copy(reader, writer).await;
}
最后修改 August 23, 2026: 更新 (499855b16)