]> git.proxmox.com Git - rustc.git/blame - src/doc/rustdoc/src/linking-to-items-by-name.md
Update (un)suspicious files
[rustc.git] / src / doc / rustdoc / src / linking-to-items-by-name.md
CommitLineData
1b1a35ee
XL
1# Linking to items by name
2
29967ef6 3Rustdoc is capable of directly linking to other rustdoc pages using the path of
6a06907d 4the item as a link. This is referred to as an 'intra-doc link'.
1b1a35ee
XL
5
6For example, in the following code all of the links will link to the rustdoc page for `Bar`:
7
8```rust
9/// This struct is not [Bar]
10pub struct Foo1;
11
12/// This struct is also not [bar](Bar)
13pub struct Foo2;
14
15/// This struct is also not [bar][b]
16///
17/// [b]: Bar
18pub struct Foo3;
19
20/// This struct is also not [`Bar`]
21pub struct Foo4;
22
29967ef6 23/// This struct *is* [`Bar`]!
1b1a35ee
XL
24pub struct Bar;
25```
26
6a06907d
XL
27Unlike normal Markdown, `[bar][Bar]` syntax is also supported without needing a
28`[Bar]: ...` reference link.
29
29967ef6
XL
30Backticks around the link will be stripped, so ``[`Option`]`` will correctly
31link to `Option`.
32
6a06907d
XL
33## Valid links
34
35You 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
37trait 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
29967ef6
XL
41
42You can also refer to items with generic parameters like `Vec<T>`. The link will
43resolve as if you had written ``[`Vec<T>`](Vec)``. Fully-qualified syntax (for example,
44`<Vec as IntoIterator>::into_iter()`) is [not yet supported][fqs-issue], however.
45
46[fqs-issue]: https://github.com/rust-lang/rust/issues/74563
1b1a35ee
XL
47
48```rust,edition2018
49use std::sync::mpsc::Receiver;
50
29967ef6 51/// This is a version of [`Receiver<T>`] with support for [`std::future`].
1b1a35ee
XL
52///
53/// You can obtain a [`std::future::Future`] by calling [`Self::recv()`].
54pub struct AsyncReceiver<T> {
55 sender: Receiver<T>
56}
57
58impl<T> AsyncReceiver<T> {
59 pub async fn recv() -> T {
60 unimplemented!()
61 }
62}
63```
64
6a06907d 65Rustdoc allows using URL fragment specifiers, just like a normal link:
1b1a35ee
XL
66
67```rust
29967ef6 68/// This is a special implementation of [positional parameters].
1b1a35ee
XL
69///
70/// [positional parameters]: std::fmt#formatting-parameters
71struct MySpecialFormatter;
72```
73
6a06907d
XL
74## Namespaces and Disambiguators
75
76Paths in Rust have three namespaces: type, value, and macro. Item names must be unique within
77their namespace, but can overlap with items in other namespaces. In case of ambiguity,
78rustdoc will warn about the ambiguity and suggest a disambiguator.
1b1a35ee
XL
79
80```rust
81/// See also: [`Foo`](struct@Foo)
82struct Bar;
83
84/// This is different from [`Foo`](fn@Foo)
85struct Foo {}
86
87fn Foo() {}
88```
89
6a06907d
XL
90These prefixes will be stripped when displayed in the documentation, so `[struct@Foo]` will be
91rendered as `Foo`.
92
29967ef6
XL
93You can also disambiguate for functions by adding `()` after the function name,
94or for macros by adding `!` after the macro name:
95
96```rust
6a06907d
XL
97/// This is different from [`foo!`]
98fn foo() {}
29967ef6 99
6a06907d
XL
100/// This is different from [`foo()`]
101macro_rules! foo {
102 () => {}
103}
104```
29967ef6 105
6a06907d
XL
106## Warnings, re-exports, and scoping
107
108Links are resolved in the scope of the module where the item is defined, even
109when the item is re-exported. If a link from another crate fails to resolve, no
110warning is given.
111
112```rust,edition2018
113mod inner {
114 /// Link to [f()]
115 pub struct S;
116 pub fn f() {}
117}
118pub use inner::S; // the link to `f` will still resolve correctly
29967ef6
XL
119```
120
6a06907d
XL
121When re-exporting an item, rustdoc allows adding additional documentation to it.
122That additional documentation will be resolved in the scope of the re-export, not
123the original, allowing you to link to items in the new crate. The new links
124will still give a warning if they fail to resolve.
125
126```rust
127/// See also [foo()]
128pub use std::process::Command;
129
130pub fn foo() {}
131```
132
133This is especially useful for proc-macros, which must always be defined in their own dedicated crate.
134
135Note: 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
137module it is defined in.
138
139If links do not look 'sufficiently like' an intra-doc link, they will be ignored and no warning
140will be given, even if the link fails to resolve. For example, any link containing `/` or `[]`
141characters will be ignored.
29967ef6
XL
142
143[#72243]: https://github.com/rust-lang/rust/issues/72243