Skip to content

Commit a9339ed

Browse files
authored
Rollup merge of #80874 - jyn514:intra-doc-docs, r=Manishearth
Update intra-doc link documentation to match the implementation r? `@Manishearth` cc `@camelid` `@m-ou-se` Relevant PRs: - #74489 - #80181 - #76078 - #77519 - #73101 Relevant issues: - #78800 - #77200 - #77199 / #54191 I haven't documented things that I consider 'just bugs', like #77732, but I have documented features that aren't implemented, like #78800.
2 parents bc5669e + c2694f1 commit a9339ed

File tree

1 file changed

+62
-13
lines changed

1 file changed

+62
-13
lines changed

src/doc/rustdoc/src/linking-to-items-by-name.md

+62-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Linking to items by name
22

33
Rustdoc is capable of directly linking to other rustdoc pages using the path of
4-
the item as a link.
4+
the item as a link. This is referred to as an 'intra-doc link'.
55

66
For example, in the following code all of the links will link to the rustdoc page for `Bar`:
77

@@ -24,11 +24,20 @@ pub struct Foo4;
2424
pub struct Bar;
2525
```
2626

27+
Unlike normal Markdown, `[bar][Bar]` syntax is also supported without needing a
28+
`[Bar]: ...` reference link.
29+
2730
Backticks around the link will be stripped, so ``[`Option`]`` will correctly
2831
link to `Option`.
2932

30-
You can refer to anything in scope, and use paths, including `Self`, `self`,
31-
`super`, and `crate`. You may also use `foo()` and `foo!()` to refer to methods/functions and macros, respectively.
33+
## Valid links
34+
35+
You can refer to anything in scope, and use paths, including `Self`, `self`, `super`, and
36+
`crate`. Associated items (functions, types, and constants) are supported, but [not for blanket
37+
trait implementations][#79682]. Rustdoc also supports linking to all primitives listed in
38+
[the standard library documentation](../std/index.html#primitives).
39+
40+
[#79682]: https://github.com/rust-lang/rust/pull/79682
3241

3342
You can also refer to items with generic parameters like `Vec<T>`. The link will
3443
resolve as if you had written ``[`Vec<T>`](Vec)``. Fully-qualified syntax (for example,
@@ -53,7 +62,7 @@ impl<T> AsyncReceiver<T> {
5362
}
5463
```
5564

56-
You can also link to sections using URL fragment specifiers:
65+
Rustdoc allows using URL fragment specifiers, just like a normal link:
5766

5867
```rust
5968
/// This is a special implementation of [positional parameters].
@@ -62,9 +71,11 @@ You can also link to sections using URL fragment specifiers:
6271
struct MySpecialFormatter;
6372
```
6473

65-
Paths in Rust have three namespaces: type, value, and macro. Item names must be
66-
unique within their namespace, but can overlap with items outside of their
67-
namespace. In case of ambiguity, rustdoc will warn about the ambiguity and ask you to disambiguate, which can be done by using a prefix like `struct@`, `enum@`, `type@`, `trait@`, `union@`, `const@`, `static@`, `value@`, `fn@`, `function@`, `mod@`, `module@`, `method@`, `prim@`, `primitive@`, `macro@`, or `derive@`:
74+
## Namespaces and Disambiguators
75+
76+
Paths in Rust have three namespaces: type, value, and macro. Item names must be unique within
77+
their namespace, but can overlap with items in other namespaces. In case of ambiguity,
78+
rustdoc will warn about the ambiguity and suggest a disambiguator.
6879

6980
```rust
7081
/// See also: [`Foo`](struct@Foo)
@@ -76,19 +87,57 @@ struct Foo {}
7687
fn Foo() {}
7788
```
7889

90+
These prefixes will be stripped when displayed in the documentation, so `[struct@Foo]` will be
91+
rendered as `Foo`.
92+
7993
You can also disambiguate for functions by adding `()` after the function name,
8094
or for macros by adding `!` after the macro name:
8195

8296
```rust
83-
/// See also: [`Foo`](struct@Foo)
84-
struct Bar;
97+
/// This is different from [`foo!`]
98+
fn foo() {}
8599

86-
/// This is different from [`Foo()`]
87-
struct Foo {}
100+
/// This is different from [`foo()`]
101+
macro_rules! foo {
102+
() => {}
103+
}
104+
```
88105

89-
fn Foo() {}
106+
## Warnings, re-exports, and scoping
107+
108+
Links are resolved in the scope of the module where the item is defined, even
109+
when the item is re-exported. If a link from another crate fails to resolve, no
110+
warning is given.
111+
112+
```rust,edition2018
113+
mod inner {
114+
/// Link to [f()]
115+
pub struct S;
116+
pub fn f() {}
117+
}
118+
pub use inner::S; // the link to `f` will still resolve correctly
90119
```
91120

92-
Note: Because of how `macro_rules!` macros are scoped in Rust, the intra-doc links of a `macro_rules!` macro will be resolved [relative to the crate root][#72243], as opposed to the module it is defined in.
121+
When re-exporting an item, rustdoc allows adding additional documentation to it.
122+
That additional documentation will be resolved in the scope of the re-export, not
123+
the original, allowing you to link to items in the new crate. The new links
124+
will still give a warning if they fail to resolve.
125+
126+
```rust
127+
/// See also [foo()]
128+
pub use std::process::Command;
129+
130+
pub fn foo() {}
131+
```
132+
133+
This is especially useful for proc-macros, which must always be defined in their own dedicated crate.
134+
135+
Note: Because of how `macro_rules!` macros are scoped in Rust, the intra-doc links of a
136+
`macro_rules!` macro will be resolved [relative to the crate root][#72243], as opposed to the
137+
module it is defined in.
138+
139+
If links do not look 'sufficiently like' an intra-doc link, they will be ignored and no warning
140+
will be given, even if the link fails to resolve. For example, any link containing `/` or `[]`
141+
characters will be ignored.
93142

94143
[#72243]: https://github.com/rust-lang/rust/issues/72243

0 commit comments

Comments
 (0)