Skip to content

Make all code block of the documentation of a single item share the same scope #70543

Closed
@robinmoussu

Description

@robinmoussu

Describe the problem you are trying to solve
When documenting a single item, if there is more than one code-block, compile and run (with cargo test) all snippets at once. This way, it become possible to share variable between multiple blocks. It think it would be useful when:

  • you have something long (and boring) to set-up, like a small graph, and want re-use it.
/// Common setup:
/// ```
/// use ...
/// let graph = create_graph();
/// let a = graph.add_node();
/// let b = graph.add_node();
/// let c = graph.add_node();
/// ```
/// First use-case:
/// ```
/// foo(graph, a, true);
/// ```
/// Second use-case:
/// ```
/// foo(graph, b, false);
/// ```
pub fn foo(graph: G, node: G::node, b: bool);
  • you have a multi-part algorithm, and you want to use regular text instead of comments to explain it.
//// First create a foo
/// ```
/// let foo = create_foo()
/// ```
/// Then pass it to bar, with a frob transformation:
/// ```
/// bar(foo, add_frob())
/// ```

Describe the solution you'd like

I see two possible solution.

  • Either all snippet documenting a single item would share the same scope automatically. It requires no modification from the user (and would be the easier to use. But I don't know if it can break existing tests (due to the ? operator most probably).
  • Or manually, with an annotation after the ```, For example ```extend_scope.

Notes
I am not fully certain that this feature request should be done here, or in rustc itself. Please tell me if I need to report it elsewhere.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-doctestsArea: Documentation tests, run by rustdocC-feature-requestCategory: A feature request, i.e: not implemented / a PR.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