You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
You can also refer to items with generic parameters like `Vec<T>`. The link will
34
43
resolve as if you had written ``[`Vec<T>`](Vec)``. Fully-qualified syntax (for example,
@@ -53,7 +62,7 @@ impl<T> AsyncReceiver<T> {
53
62
}
54
63
```
55
64
56
-
You can also link to sections using URL fragment specifiers:
65
+
Rustdoc allows using URL fragment specifiers, just like a normal link:
57
66
58
67
```rust
59
68
/// This is a special implementation of [positional parameters].
@@ -62,9 +71,11 @@ You can also link to sections using URL fragment specifiers:
62
71
structMySpecialFormatter;
63
72
```
64
73
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.
68
79
69
80
```rust
70
81
/// See also: [`Foo`](struct@Foo)
@@ -76,19 +87,57 @@ struct Foo {}
76
87
fnFoo() {}
77
88
```
78
89
90
+
These prefixes will be stripped when displayed in the documentation, so `[struct@Foo]` will be
91
+
rendered as `Foo`.
92
+
79
93
You can also disambiguate for functions by adding `()` after the function name,
80
94
or for macros by adding `!` after the macro name:
81
95
82
96
```rust
83
-
///See also: [`Foo`](struct@Foo)
84
-
structBar;
97
+
///This is different from [`foo!`]
98
+
fnfoo() {}
85
99
86
-
/// This is different from [`Foo()`]
87
-
structFoo {}
100
+
/// This is different from [`foo()`]
101
+
macro_rules!foo {
102
+
() => {}
103
+
}
104
+
```
88
105
89
-
fnFoo() {}
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
90
119
```
91
120
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
+
pubusestd::process::Command;
129
+
130
+
pubfnfoo() {}
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 `[]`
0 commit comments