How do I properly write macro documentation if the

2019-02-23 22:37发布

问题:

I defined a macro in a module, and it works fine. Now, I'm trying to document said macro with an example. Apparently, I need to manually specify the crate line to ask for macros:

/// ```
/// # #[macro_use] extern crate foo;
/// // Some code
/// ```

However, I now get an error saying:

error: an `extern crate` loading macros must be at the crate root

Apparently the example code is loaded in the macro's module, and does not seem compatible with macro_use...

I can't believe everyone writes macros directly in the root module... right?

回答1:

Well adding a main function did the trick. My example code did not need to run anything (just compile) so I didn't even bother adding a main function, but apparently adding it puts the code in a virtual "crate root", and it accepts the macro_use. Yay!

So what I did is just add :

/// # fn main() { }