不能在子子模块范围内使用mongodb crate中的`doc!`宏

时间:2017-07-11 11:50:45

标签: rust rust-cargo

我使用以下文件结构:

├── 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.
    }
}

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:-1)

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

use mongodb::bson::doc
相关问题