rust - tokio-rs 文档示例未编译
问题描述
我正在尝试通过阅读他们网站上的文档来学习 tokio-rs,但是这里的示例没有编译。我开始追查错误以修复它们,但它从文档示例中改变了很多东西,我开始认为我做错了什么。
这是文档中的一个示例:
use tokio::net::tcp::TcpStream;
use tokio::prelude::*;
let addr = "127.0.0.1:12345".parse().unwrap();
let read_8_fut = TcpStream::connect(&addr)
.and_then(|stream| {
// We need to create a buffer for read_exact to write into.
// A Vec<u8> is a good starting point.
// read_exact will read buffer.len() bytes, so we need
// to make sure the Vec isn't empty!
let mut buf = vec![0; 8];
// read_exact returns a Future that resolves when
// buffer.len() bytes have been read from stream.
tokio::io::read_exact(stream, buf)
})
.inspect(|(_stream, buf)| {
// Notice that we get both the buffer and the stream back
// here, so that we can now continue using the stream to
// send a reply for example.
println!("got eight bytes: {:x?}", buf);
});
// We can now either chain more futures onto read_8_fut,
// or if all we wanted to do was read and print those 8
// bytes, we can just use tokio::run to run it (taking
// care to map Future::Item and Future::Error to ()).
我的 Corgo.toml 文件中有这个:
[dependencies]
tokio = { version = "0.2.9", features = ["full"] }
但是,我得到了这些编译器错误:
[Sat Jan 11:username]~/RustProjects/tokio_example$ cargo test
Compiling tokio_example v0.1.0 (/Users/username/RustProjects/tokio_example)
error[E0425]: cannot find function `read_exact` in module `tokio::io`
--> src/main.rs:16:15
|
16 | tokio::io::read_exact(stream, buf)
| ^^^^^^^^^^ not found in `tokio::io`
error[E0603]: struct `TcpStream` is private
--> src/main.rs:1:22
|
1 | use tokio::net::tcp::TcpStream;
| ^^^^^^^^^
warning: unused import: `tokio::prelude::*`
--> src/main.rs:2:5
|
2 | use tokio::prelude::*;
| ^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0425, E0603.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `tokio_example`.
To learn more, run the command again with --verbose.
解决方案
推荐阅读
- c++ - 如何生成一个 std::shared_ptr 与其控制块具有局部性但又是另一个类的聚合?
- php - 如何通过laravel表单传递带逗号的输入文本?
- html - 尽管仅在正常状态下指定,为什么转换仍适用于活动状态
- javascript - 纹理加载图像 url 的域返回错误
- python - 在 Docker 映像的预提交挂钩中为 python 2.7 和 3.7 运行 pylint
- python - 基于条件的数据框生成器 Pandas
- swagger - 如何使用 Swashbuckle.AspNetCore 为我的 Swagger 文档和 Swagger UI 要求 HTTPS?
- if-statement - 当 IMPORTRANGE 单元格为空白时,用替代文本填充单元格
- javascript - V-for 点击所有按钮而不是一个按钮
- swift - 尽管使用了完成块,但仍无法处理与数据检索相关的 Firebase 异步 API 调用问题