How to embed a Rust macro variable into documentation?

后端 未结 1 1734
星月不相逢
星月不相逢 2020-12-07 02:27

I\'d like to use a macro variable in the macro-generated documentation:

macro_rules! impl_foo {
    ($name:ident) => {
        /// Returns a new `$name`.
         


        
相关标签:
1条回答
  • 2020-12-07 02:43

    This can be done using a recursive macro:

    macro_rules! impl_foo {
        ($name:ident, $sname:expr) => {
            #[doc = "Returns a new `"]
            #[doc = $sname]
            #[doc = "`."]
            pub fn myfoo() -> $name {
                42
            }
        };
    
        ($name:tt) => {
            impl_foo!($name, stringify!($name));
        };
    }
    
    impl_foo!(u32);
    
    
    fn main() {
        println!("Hello, world!");
    }
    

    Which renders as:

    0 讨论(0)
提交回复
热议问题