1

I use following file structure:

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

My main.rs file imports the stuff like:

#[macro_use]
extern crate mongodb;

mod models;

My user.rs file looks like:

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

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

How can I use my doc! macro in the user.rs file? I also tried to add #[macro_use] to the stuff like mod models;, but nothing worked.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Daniel O
  • 365
  • 1
  • 2
  • 7
  • You say `!doc`, but macros are written as `doc!`. If that's not the issue, you have not provided anywhere *near* enough information. We need to see the code that's failing to compile, and the output of the compiler. – DK. Jul 11 '17 at 12:00
  • @DK Sorry about that. But I wrote doc! in my code – Daniel O Jul 11 '17 at 12:02

2 Answers2

2

The mongodb crate (version 0.3.1) has no such macro. The bson crate (version 0.9.0), a dependency of mongodb, does. You need to declare that and import from there:

#[macro_use]
extern crate bson;
extern crate mongodb;
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
-1

The mongodb crate (version 1.1.1) re-exports bson. In Rust 2018 you can write

use mongodb::bson::doc