rustrust-cargorustdoc

How to hide Statics and Functions generated by the lazy_static crate from cargo doc?


I was documenting a module for a rust project that contains 2 lazy_static macro invocations and when evaluating the documentation generated by cargo doc I saw that there was a bunch of Structs, Statics and Functions with a lock beside them that are not in the source code. Is there a way to hide them from the documentation that cargo doc generates? Picture of problem.

I tried adding the #[doc(hidden)] attribute to the invocation of the lazy_static macro and it did not work. It did work for the structs generated by lazy_static (which where regular expressions in this case), the appearance of a LAZY struct for each struct would disappear when adding the attribute but the same did not happen when I tried adding it above the macro invocation. I was expecting it to work but instead I get a unused attribute doc.


Solution

  • You can use a module to completely hide these from documentation:

    #[doc(hidden)]
    mod statics {
        use lazy_static::lazy_static;
    
        lazy_static! {
            pub static ref THING: Vec<u8> = vec![1, 2, 3];
        }
    }
    use statics::*;
    

    But I'd recommend using once_cell::sync::Lazy instead, as it is being incorporated into the standard library as std::sync::LazyLock:

    use once_cell::sync::Lazy;
    
    static THING: Lazy<Vec<u8>> = Lazy::new(|| vec![1, 3, 3]);