]>
Commit | Line | Data |
---|---|---|
1b1a35ee XL |
1 | # Linking to items by name |
2 | ||
29967ef6 | 3 | Rustdoc is capable of directly linking to other rustdoc pages using the path of |
6a06907d | 4 | the item as a link. This is referred to as an 'intra-doc link'. |
1b1a35ee XL |
5 | |
6 | For 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] | |
10 | pub struct Foo1; | |
11 | ||
12 | /// This struct is also not [bar](Bar) | |
13 | pub struct Foo2; | |
14 | ||
15 | /// This struct is also not [bar][b] | |
16 | /// | |
17 | /// [b]: Bar | |
18 | pub struct Foo3; | |
19 | ||
20 | /// This struct is also not [`Bar`] | |
21 | pub struct Foo4; | |
22 | ||
29967ef6 | 23 | /// This struct *is* [`Bar`]! |
1b1a35ee XL |
24 | pub struct Bar; |
25 | ``` | |
26 | ||
6a06907d XL |
27 | Unlike normal Markdown, `[bar][Bar]` syntax is also supported without needing a |
28 | `[Bar]: ...` reference link. | |
29 | ||
29967ef6 XL |
30 | Backticks around the link will be stripped, so ``[`Option`]`` will correctly |
31 | link to `Option`. | |
32 | ||
6a06907d XL |
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 | |
29967ef6 XL |
41 | |
42 | You can also refer to items with generic parameters like `Vec<T>`. The link will | |
43 | resolve 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 | |
49 | use 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()`]. | |
54 | pub struct AsyncReceiver<T> { | |
55 | sender: Receiver<T> | |
56 | } | |
57 | ||
58 | impl<T> AsyncReceiver<T> { | |
59 | pub async fn recv() -> T { | |
60 | unimplemented!() | |
61 | } | |
62 | } | |
63 | ``` | |
64 | ||
6a06907d | 65 | Rustdoc 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 | |
71 | struct MySpecialFormatter; | |
72 | ``` | |
73 | ||
6a06907d XL |
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. | |
1b1a35ee XL |
79 | |
80 | ```rust | |
81 | /// See also: [`Foo`](struct@Foo) | |
82 | struct Bar; | |
83 | ||
84 | /// This is different from [`Foo`](fn@Foo) | |
85 | struct Foo {} | |
86 | ||
87 | fn Foo() {} | |
88 | ``` | |
89 | ||
6a06907d XL |
90 | These prefixes will be stripped when displayed in the documentation, so `[struct@Foo]` will be |
91 | rendered as `Foo`. | |
92 | ||
29967ef6 XL |
93 | You can also disambiguate for functions by adding `()` after the function name, |
94 | or for macros by adding `!` after the macro name: | |
95 | ||
96 | ```rust | |
6a06907d XL |
97 | /// This is different from [`foo!`] |
98 | fn foo() {} | |
29967ef6 | 99 | |
6a06907d XL |
100 | /// This is different from [`foo()`] |
101 | macro_rules! foo { | |
102 | () => {} | |
103 | } | |
104 | ``` | |
29967ef6 | 105 | |
6a06907d XL |
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 | |
29967ef6 XL |
119 | ``` |
120 | ||
6a06907d XL |
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. | |
29967ef6 XL |
142 | |
143 | [#72243]: https://github.com/rust-lang/rust/issues/72243 |