Skip to content

Commit 997bf0e

Browse files
authored
Rollup merge of rust-lang#74184 - Manishearth:doc-intra-doc, r=GuillaumeGomez
Add docs for intra-doc-links Fixes rust-lang#66000 Hmm, for some reason my push closed the previous PR
2 parents 71da131 + 36a229b commit 997bf0e

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

src/doc/rustdoc/src/unstable-features.md

+42-30
Original file line numberDiff line numberDiff line change
@@ -38,50 +38,62 @@ future.
3838
Attempting to use these error numbers on stable will result in the code sample being interpreted as
3939
plain text.
4040

41-
### Linking to items by type
41+
### Linking to items by name
4242

43-
As designed in [RFC 1946], Rustdoc can parse paths to items when you use them as links. To resolve
44-
these type names, it uses the items currently in-scope, either by declaration or by `use` statement.
45-
For modules, the "active scope" depends on whether the documentation is written outside the module
46-
(as `///` comments on the `mod` statement) or inside the module (at `//!` comments inside the file
47-
or block). For all other items, it uses the enclosing module's scope.
43+
Rustdoc is capable of directly linking to other rustdoc pages in Markdown documentation using the path of item as a link.
4844

49-
[RFC 1946]: https://github.com/rust-lang/rfcs/pull/1946
50-
51-
For example, in the following code:
45+
For example, in the following code all of the links will link to the rustdoc page for `Bar`:
5246

5347
```rust
54-
/// Does the thing.
55-
pub fn do_the_thing(_: SomeType) {
56-
println!("Let's do the thing!");
57-
}
48+
/// This struct is not [Bar]
49+
pub struct Foo1;
50+
51+
/// This struct is also not [bar](Bar)
52+
pub struct Foo2;
53+
54+
/// This struct is also not [bar][b]
55+
///
56+
/// [b]: Bar
57+
pub struct Foo3;
58+
59+
/// This struct is also not [`Bar`]
60+
pub struct Foo4;
5861

59-
/// Token you use to [`do_the_thing`].
60-
pub struct SomeType;
62+
pub struct Bar;
6163
```
6264

63-
The link to ``[`do_the_thing`]`` in `SomeType`'s docs will properly link to the page for `fn
64-
do_the_thing`. Note that here, rustdoc will insert the link target for you, but manually writing the
65-
target out also works:
65+
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.
6666

67-
```rust
68-
pub mod some_module {
69-
/// Token you use to do the thing.
70-
pub struct SomeStruct;
71-
}
67+
```rust,edition2018
68+
use std::sync::mpsc::Receiver;
7269
73-
/// Does the thing. Requires one [`SomeStruct`] for the thing to work.
70+
/// This is an version of [`Receiver`], with support for [`std::future`].
7471
///
75-
/// [`SomeStruct`]: some_module::SomeStruct
76-
pub fn do_the_thing(_: some_module::SomeStruct) {
77-
println!("Let's do the thing!");
72+
/// You can obtain a [`std::future::Future`] by calling [`Self::recv()`].
73+
pub struct AsyncReceiver<T> {
74+
sender: Receiver<T>
75+
}
76+
77+
impl<T> AsyncReceiver<T> {
78+
pub async fn recv() -> T {
79+
unimplemented!()
80+
}
7881
}
7982
```
8083

81-
For more details, check out [the RFC][RFC 1946], and see [the tracking issue][43466] for more
82-
information about what parts of the feature are available.
84+
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@`:
85+
86+
```rust
87+
/// See also: [`Foo`](struct@Foo)
88+
struct Bar;
89+
90+
/// This is different from [`Foo`](fn@Foo)
91+
struct Foo {}
92+
93+
fn Foo() {}
94+
```
8395

84-
[43466]: https://github.com/rust-lang/rust/issues/43466
96+
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.
8597

8698
## Extensions to the `#[doc]` attribute
8799

0 commit comments

Comments
 (0)