Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: Add option to hide imports from rendered examples #109121

Open
lcmgh opened this issue Mar 14, 2023 · 5 comments
Open

rustdoc: Add option to hide imports from rendered examples #109121

lcmgh opened this issue Mar 14, 2023 · 5 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@lcmgh
Copy link

lcmgh commented Mar 14, 2023

Problem

Adding imports to code examples are necessary to make them compile however it makes code examples more hard to read. That's why many developers choose to hide them from the generated output. This leads to the problem that when copying code examples users have to manually choose the correct imports which can be error-prone.

Proposed Solution

Add an UI option to each Rust code block that allows to dynamically hide and show the imports.

Notes

This may raise the question if we also want an option to show/hide all hidden code fragments.

@lcmgh lcmgh added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Mar 14, 2023
@lcmgh lcmgh changed the title cargo doc: Add option to hide imports from examples cargo doc: Add option to hide imports from rendered examples Mar 14, 2023
@ehuss ehuss transferred this issue from rust-lang/cargo Mar 14, 2023
@ehuss
Copy link
Contributor

ehuss commented Mar 14, 2023

Transferred to rust-lang/rust as this sounds like a rustdoc issue, and that is where rustdoc issues are tracked.

@ehuss ehuss added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Mar 14, 2023
@jyn514
Copy link
Member

jyn514 commented Mar 14, 2023

you can already do this today: https://doc.rust-lang.org/beta/rustdoc/write-documentation/documentation-tests.html#hiding-portions-of-the-example

if you want a button that users can click to show or hide it, that's harder, but i'm not sure why it would be useful?

@jyn514 jyn514 changed the title cargo doc: Add option to hide imports from rendered examples rustdoc: Add option to hide imports from rendered examples Mar 14, 2023
@jsha
Copy link
Contributor

jsha commented Mar 15, 2023

This sounds like a Curse of Knowledge issue :-D. Doc authors hide imports because the needed imports are obvious to the author, but they are sometimes not obvious to the reader.

It would probably be good to add to the rustdoc book, under the "How to write documentation > What to include (and exclude) > Examples" section, a recommendation to include any necessary use statements in the first example for a given item (including for each method), and optionally hide them for brevity in subsequent examples.

@lcmgh
Copy link
Author

lcmgh commented Mar 15, 2023

if you want a button that users can click to show or hide it, that's harder, but i'm not sure why it would be useful?

This helps users to understand examples quickly without getting distracted by imports while still providing an option to copy a fully working code example if they decide to use it.

@GuillaumeGomez
Copy link
Member

I had a PR which proposed this change a while ago: #86892

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants