【问题标题】:Cannot use the `doc!` macro from the mongodb crate in the scope of a sub sub module无法在子子模块的范围内使用 mongodb crate 中的“doc!”宏
【发布时间】:2017-07-11 11:50:45
【问题描述】:

我使用以下文件结构:

├── src
│   ├── main.rs     // Macros from here
│   ├── models
│   │   ├── mod.rs  // Loads the user.rs file
│   │   └── user.rs // Should be visible here
├── Cargo.toml

我的main.rs 文件导入如下内容:

#[macro_use]
extern crate mongodb;

mod models;

我的user.rs 文件如下所示:

pub struct User {
    username: String,
    password: String,
}

impl User {
    fn create_doc() {
        // Some code, but doc! from crate mongodb is not in this scope.
    }
}

如何在user.rs 文件中使用我的doc! 宏?我还尝试将#[macro_use] 添加到mod models; 之类的内容中,但没有任何效果。

【问题讨论】:

  • 你说!doc,但是宏写成doc!。如果这不是问题,那么您没有提供任何接近足够的信息。我们需要查看编译失败的代码,以及编译器的输出。
  • @DK 抱歉。但是我写了文档!在我的代码中

标签: rust rust-cargo


【解决方案1】:

mongodb crate(版本 0.3.1)has no such macrobson crate (version 0.9.0) 是 mongodb 的依赖项。您需要声明并从那里导入:

#[macro_use]
extern crate bson;
extern crate mongodb;

【讨论】:

  • 非常感谢。从来没想过。
【解决方案2】:

mongodb crate(版本 1.1.1)重新导出 bson。 在 Rust 2018 中,您可以编写

use mongodb::bson::doc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多