【发布时间】:2019-05-31 23:52:25
【问题描述】:
我正在尝试理解 Rust 中的异步 I/O。以下代码基于 Katharina Fey 的 sn-p Jan 2019 talk 对我有用:
use futures::future::Future;
use std::io::BufReader;
use tokio::io::*;
fn main() {
let reader = BufReader::new(tokio::io::stdin());
let buffer = Vec::new();
println!("Type something:");
let fut = tokio::io::read_until(reader, b'\n', buffer)
.and_then(move |(stdin, buffer)| {
tokio::io::stdout()
.write_all(&buffer)
.map_err(|e| panic!(e))
})
.map_err(|e| panic!(e));
tokio::run(fut);
}
在找到该代码之前,我尝试从 read_until 文档中找出它。
如何解释read_until 的签名以在上述代码示例中使用它?
pub fn read_until<A>(a: A, byte: u8, buf: Vec<u8>) -> ReadUntil<A>
where
A: AsyncRead + BufRead,
具体来说,通过阅读文档,我如何知道传入and_then闭包的参数和预期结果是什么?
【问题讨论】:
标签: rust rust-tokio