首页 > 解决方案 > 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.

标签: rustrust-tokio

解决方案


推荐阅读