【问题标题】:Error E0412: use of undeclared crate or module `tokio` when importing tokio::sync错误 E0412:导入 tokio::sync 时使用未声明的 crate 或模块 `tokio`
【发布时间】:2021-05-20 17:57:31
【问题描述】:

在我的 rust 项目中,cargo 抱怨使用 tokio::sync 时使用的类型不在范围内:

use tokio::sync::RwLock;
  |     ^^^^^ use of undeclared crate or module `tokio`

但显然在范围内:

use tokio::sync::RwLock;

#[derive(Deserialize, Serialize)]
pub struct MyStruct {
    some_field: RwLock<SomeType>
}

根据文档,同步模块只有在启用sync 功能标志后才可用,我已经显式和隐式地做到了这一点:

tokio = { version = "0.3", features = ["macros", "sync"] }
tokio = { version = "0.3", features = ["full"] }

但这两者都不起作用。

【问题讨论】:

  • 您是否在main.rslib.rs 中添加了extern crate tokio; 声明?
  • 是的,但是我 :face-palm: 弄清楚了为什么我会收到这个错误。我只有在 dev-dependencies 下的 tokio。

标签: rust rust-tokio


【解决方案1】:

我没有注意到我在dev-dependencies下只有tokio,这就是cargo无法编译项目的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 2017-10-30
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多