How do I properly write macro documentation if the macro is defined in a module?

◇◆丶佛笑我妖孽 提交于 2019-12-20 01:14:26

问题


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() { }


来源:https://stackoverflow.com/questions/30651196/how-do-i-properly-write-macro-documentation-if-the-macro-is-defined-in-a-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!