Skip to content

Commit 058b72e

Browse files
committed
Add docs for intra-doc-links
1 parent 394e1b4 commit 058b72e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/doc/rustdoc/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
- [Documentation tests](documentation-tests.md)
88
- [Lints](lints.md)
99
- [Passes](passes.md)
10+
- [Intra-doc links](intra-doc-links.md)
1011
- [Advanced Features](advanced-features.md)
1112
- [Unstable features](unstable-features.md)
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Intra-doc links
2+
3+
_This feature is currently nightly-only_
4+
5+
Rustdoc is capable of directly linking to other rustdoc pages in Markdown documentation using the path of item as a link.
6+
7+
For example, in the following code all of the links will link to the rustdoc page for `Bar`:
8+
9+
```rust
10+
11+
/// This struct is not [Bar]
12+
pub struct Foo1;
13+
14+
/// This struct is also not [bar](Bar)
15+
pub struct Foo2;
16+
17+
/// This struct is also not [bar][b]
18+
///
19+
/// [b]: Bar
20+
pub struct Foo3;
21+
22+
/// This struct is also not [`Bar`]
23+
pub struct Foo4;
24+
25+
pub struct Bar;
26+
```
27+
28+
You can refer to anything in scope, and use paths, including `Self`. You may also use `foo()` and `foo!()` to refer to methods/functions and macros respectively.
29+
30+
```rust,edition2018
31+
use std::sync::mpsc::Receiver;
32+
33+
/// This is an version of [`Receiver`], with support for [`std::future`].
34+
///
35+
/// You can obtain a [`std::future::Future`] by calling [`Self::recv()`].
36+
pub struct AsyncReceiver<T> {
37+
sender: Receiver<T>
38+
}
39+
40+
impl<T> AsyncReceiver<T> {
41+
pub async fn recv() -> T {
42+
unimplemented!()
43+
}
44+
}
45+
```
46+
47+
Paths in Rust have three namespaces: type, value, and macro. Items from these namespaces are allowed to overlap. 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@`, `function@`, `mod@`, `fn@`, `module@`, `method@` , `macro@`, or `derive@`:
48+
49+
```rust
50+
/// See also: [`Foo`](struct@Foo)
51+
struct Bar;
52+
53+
/// This is different from [`Foo`](fn@Foo)
54+
struct Foo {}
55+
56+
fn Foo() {}
57+
```
58+
59+
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, as opposed to the module it is defined in.

0 commit comments

Comments
 (0)