rust - 无法在子模块范围内使用 mongodb crate 中的 `doc!` 宏

标签 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.
    }
}

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

最佳答案

mongodb crate(版本 0.3.1)has no such macro . bson crate (version 0.9.0) ,mongodb 的依赖项,确实如此。您需要声明并从那里导入:

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

关于rust - 无法在子模块范围内使用 mongodb crate 中的 `doc!` 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45033782/

相关文章:

rust - Rust 中元组的求值顺序是什么?

rust - 无法在适用于 Linux 的 Windows 子系统上使用 Rust 1.25 构建 actix-web 0.7.8

compiler-errors - 如何添加外部软件包并在rust编译器中运行?

rust - 为什么可以将带连字符的 crate 名称更改为带下划线的名称,在这种模棱两可的情况下命名规则是什么?

macos - 为什么Rust会发出280行汇编代码来打印 “Hello, world”?

rust - 如何从 Rust 中的特征获取函数指针?

optimization - 我可以在 Rust 中按值有效地返回对象吗?

error-handling - 源错误是否应该在显示输出中包含该源?

rust - 无法使用git存储库中的alacritty依赖关系

opencv - Rust 与 OpenCV 的奇怪行为