Skip to content

Rustdoc: transpose links-containing-code into code-containing-links #131845

@dtolnay

Description

@dtolnay

For example, consider this documentation on i32::unchecked_add:
https://doc.rust-lang.org/1.82.0/std/primitive.i32.html#method.unchecked_add

/// Calling `x.unchecked_add(y)` is semantically equivalent to calling
/// `x.`[`checked_add`]`(y).`[`unwrap_unchecked`]`()`.

Rustdoc currently produces the following HTML. (I have inserted newlines for readability. These are not present in rustdoc's output.)

<code>x.</code>
<a href="primitive.i32.html#method.checked_add"><code>checked_add</code></a>
<code>(y).</code>
<a href="option/enum.Option.html#method.unwrap_unchecked"><code>unwrap_unchecked</code></a>
<code>()</code>

Because there are 5 individual code blocks, there is weird padding between their otherwise-monospaced text, and inappropriately placed rounded corners.

It would be better to produce, from exactly the above Markdown source code, the following HTML structure instead.

<code>
x.
<a href="primitive.i32.html#method.checked_add">checked_add</a>
(y).
<a href="option/enum.Option.html#method.unwrap_unchecked">unwrap_unchecked</a>
()
</code>

It is sometimes possible to achieve this output today by manually writing <code> and <a>:

/// <code>x.<a href="...">checked_add</a>(y).<a href="...">unwrap_unchecked</a>()</code>

But the reason I say "sometimes" and the reason I have tagged A-intra-doc-links Area: Intra-doc links, the ability to link to items in docs by name is that this workaround does not work if our links need to be intra-doc links. Rustdoc's intra-doc linking only kicks in for Markdown links ([`Option::unwrap_unchecked`]) and not for HTML links (<a href="Option::unwrap_unchecked"></a>). Whether or not that changes in the future as a separate issue, I still think the suggestion in this issue will remain valuable because writing inline code and links in Markdown syntax is always going to be more convenient than falling back to HTML syntax.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-intra-doc-linksArea: Intra-doc links, the ability to link to items in docs by nameA-markdown-parsingArea: Markdown parsing for doc-commentsC-enhancementCategory: An issue proposing an enhancement or a PR with one.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