Skip to content

Commit 0271791

Browse files
committed
Update intra-doc link documentation to match the implementation
1 parent c8915ee commit 0271791

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

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

+56-8
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

@@ -27,8 +27,23 @@ pub struct Bar;
2727
Backticks around the link will be stripped, so ``[`Option`]`` will correctly
2828
link to `Option`.
2929

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.
30+
## Valid links
31+
32+
You can refer to anything in scope, and use paths, including `Self`, `self`, `super`, and
33+
`crate`. Associated items (functions, types, and constants) are supported, but [not for blanket
34+
trait implementations][#79682]. Rustdoc also supports linking to the following primitives, which
35+
have no path and cannot be imported:
36+
37+
- [`slice`](https://doc.rust-lang.org/std/primitive.slice.html)
38+
- [`array`](https://doc.rust-lang.org/std/primitive.array.html)
39+
- [`tuple`](https://doc.rust-lang.org/std/primitive.tuple.html)
40+
- [`unit`](https://doc.rust-lang.org/std/primitive.unit.html)
41+
- [`fn`](https://doc.rust-lang.org/std/primitive.fn.html)
42+
- [`pointer`](https://doc.rust-lang.org/std/primitive.pointer.html), `*`, `*const`, or `*mut`
43+
- [`reference`](https://doc.rust-lang.org/std/primitive.reference.html), `&`, or `&mut`
44+
- [`never`](https://doc.rust-lang.org/std/primitive.never.html) or `!`
45+
46+
[#79682]: https://github.com/rust-lang/rust/pull/79682
3247

3348
You can also refer to items with generic parameters like `Vec<T>`. The link will
3449
resolve as if you had written ``[`Vec<T>`](Vec)``. Fully-qualified syntax (for example,
@@ -53,7 +68,7 @@ impl<T> AsyncReceiver<T> {
5368
}
5469
```
5570

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

5873
```rust
5974
/// This is a special implementation of [positional parameters].
@@ -62,9 +77,13 @@ You can also link to sections using URL fragment specifiers:
6277
struct MySpecialFormatter;
6378
```
6479

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@`:
80+
## Namespaces and Disambiguators
81+
82+
Paths in Rust have three namespaces: type, value, and macro. Item names must be unique within
83+
their namespace, but can overlap with items outside of their namespace. In case of ambiguity,
84+
rustdoc will warn about the ambiguity and ask you to disambiguate, which can be done by using a
85+
prefix like `struct@`, `enum@`, `type@`, `trait@`, `union@`, `const@`, `static@`, `value@`,
86+
`fn@`, `function@`, `mod@`, `module@`, `method@`, `prim@`, `primitive@`, `macro@`, or `derive@`:
6887

6988
```rust
7089
/// See also: [`Foo`](struct@Foo)
@@ -76,6 +95,9 @@ struct Foo {}
7695
fn Foo() {}
7796
```
7897

98+
These prefixes will be stripped when displayed in the documentation, so `[struct@Foo]`
99+
will be rendered as `Foo`.
100+
79101
You can also disambiguate for functions by adding `()` after the function name,
80102
or for macros by adding `!` after the macro name:
81103

@@ -89,6 +111,32 @@ struct Foo {}
89111
fn Foo() {}
90112
```
91113

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.
114+
## Warnings, re-exports, and scoping
115+
116+
Links are resolved in the current module scope, even when re-exported. If a link from another
117+
crate fails to resolve, no warning is given.
118+
119+
When re-exporting an item, rustdoc allows additional documentation to it. That documentation will
120+
be resolved in the new scope, not the original, allowing you to link to items in the current
121+
crate. The new links will still give a warning if they fail to resolve.
122+
123+
```rust
124+
/// See also [foo()]
125+
pub use std::process::Command;
126+
127+
pub fn foo() {}
128+
```
129+
130+
This is especially useful for proc-macros, which must always be in their own dedicated crate.
131+
132+
Note: Because of how `macro_rules!` macros are scoped in Rust, the intra-doc links of a
133+
`macro_rules!` macro will be resolved [relative to the crate root][#72243], as opposed to the
134+
module it is defined in.
135+
136+
If links do not look 'sufficiently like' an intra-doc link, they will be ignored and no warning
137+
will be given, even if the link fails to resolve. For example, any link containing `/` or `[]`
138+
characters will be ignored. You can see the full criteria for 'sufficiently like' in [the source
139+
code].
93140

94141
[#72243]: https://github.com/rust-lang/rust/issues/72243
142+
[the source code]: https://github.com/rust-lang/rust/blob/34628e5b533d35840b61c5db0665cf7633ed3c5a/src/librustdoc/passes/collect_intra_doc_links.rs#L982

0 commit comments

Comments
 (0)