rustrust-cargorustdoc

How can I prevent "cargo doc" from exporting pub(crate) items


I am trying to generate rustdoc for only my public facing API. I would like this to include pub but not pub(crate) items.

Is there a way to indicate this without having to exclude each item individually?

pub struct DocumentMe;

// I do not want to have to
// explicitly specify excluding this
#[doc(hidden)]
pub(crate) struct DoNotDocumentMe;

As @Chayim Friedman posted in the comments, this is only an issue with binary crates, and not library crates.


Solution

  • Since Rust 1.41, private APIs in binary crates are documented by default. Since the API is not exported to anyone (as this is a binary), Rust assumes that you want to document the API for consumers of the source code, so it also documents private items.

    In library crates, you can change the default and also document private items using the --document-private-items flag. Sadly, I don't think there is an option to not document private items for binary crates. In the PR that changed the default for Rust 1.41, it was said that the PR can go without a flag to change the default and that such flag can be added in the future if there is a demand for it. I don't think this ever happened.