我正在尝试使用tokios后台事件循环功能,但是我不能使用任何tokio反应堆特性,我也不知道为什么。我用cargo new tokio-test --bin创建了一个新项目,并复制粘贴了代码片段,并在链接中建立了一个基本的TCP连接。
Cargo.toml
[package]
name = "tokio-test"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1.11.0", features = ["full"] }main.rs
use tokio::prelude::*;
use tokio::net::TcpStream;
fn main() {
let addr = "93.184.216.34:9243".parse().unwrap();
let connect_future = TcpStream::connect(&addr);
let task = connect_future
.and_then(|socket| {
println!("successfully connected");
Ok(())
})
.map_err(|e| println!("failed to connect; err={:?}", e));
tokio::run(task);
}当运行cargo build时,我得到:
error[E0432]: unresolved import `tokio::prelude`
--> src/main.rs:1:12
|
1 | use tokio::prelude::*;
| ^^^^^^^ could not find `prelude` in `tokio`
error[E0425]: cannot find function `run` in crate `tokio`
--> src/main.rs:16:12
|
16 | tokio::run(task);
| ^^^ not found in `tokio`
error[E0599]: no method named `and_then` found for opaque type `impl Future` in the current scope
--> src/main.rs:10:10
|
10 | .and_then(|socket| {
| ^^^^^^^^ method not found in `impl Future`
|
help: consider `await`ing on the `Future` and calling the method on its `Output`
|
10 | .await.and_then(|socket| {
| ^^^^^^显然,我错过了tokio的一些指令,但我想不出如何包含它。我的rustc版本是rustc 1.55.0 (c8dfcfe04 2021-09-06)。
发布于 2021-09-09 18:00:35
您依赖于tokio v1.11.0 (当前版本),但是您的文档是v0.1.22。接口已经发生了很大的变化,这就是为什么您没有找到所有的类型、功能和模块。当前的文档是这里。
如果您通过Google找到文档:Google返回旧docs.rs结果的问题是众所周知的。有一个将解决这个问题的未决问题。
此外,docs.rs会在页面的左上角显示一个警告,如果您不使用该版本,就会转到最新版本。你应该养成点击它的习惯,除非你知道你使用的是旧版本。(感谢克德雷科在评论中指出了这一点)
https://stackoverflow.com/questions/69122772
复制相似问题