Skip to content

rustdoc: Document how and when rustdoc wraps a code block in a main function. #17554

Closed
@DanielKeep

Description

@DanielKeep

Currently, there is no indication in the documentation for rustdoc that it wraps code blocks in fn main() { ... }, nor how to stop it from doing so.

To give an example of how this can be very confusing, if you try to test the following code block, you get an error stating "unresolved import f1. There is no f1 in ???" which is (seemingly) not true.

pub fn f1() {}

mod a {
    use f1;
    fn f2() { f1() }
}

Preferably, the docs should note that, by default, all code blocks are wrapped in a main function, that this can be avoided by ensuring the string "fn main" appears (or whatever the actual test is; I think it just looks for a string in the input), and that # can be used to hide this from the rendered output (which is already in the docs in a different context).

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions