首页 > 解决方案 > 错误 E0412:导入 tokio::sync 时使用未声明的 crate 或模块 `tokio`

问题描述

在我的 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"] }

但它不适用于任何一个。

标签: rustrust-tokio

解决方案


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


推荐阅读