You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of rust-lang#76934 - camelid:rustdoc-allow-generic-params, r=jyn514
Allow generic parameters in intra-doc links
Fixesrust-lang#62834.
---
The contents of the generics will be mostly ignored (except for warning
if fully-qualified syntax is used, which is currently unsupported in
intra-doc links - see issue rust-lang#74563).
* Allow links like `Vec<T>`, `Result<T, E>`, and `Option<Box<T>>`
* Allow links like `Vec::<T>::new()`
* Warn on
* Unbalanced angle brackets (e.g. `Vec<T` or `Vec<T>>`)
* Missing type to apply generics to (`<T>` or `<Box<T>>`)
* Use of fully-qualified syntax (`<Vec as IntoIterator>::into_iter`)
* Invalid path separator (`Vec:<T>:new`)
* Too many angle brackets (`Vec<<T>>`)
* Empty angle brackets (`Vec<>`)
Note that this implementation *does* allow some constructs that aren't
valid in the actual Rust syntax, for example `Box::<T>new()`. That may
not be supported in rustdoc in the future; it is an implementation
detail.
Copy file name to clipboardexpand all lines: src/doc/rustdoc/src/linking-to-items-by-name.md
+35-6
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
# Linking to items by name
2
2
3
-
Rustdoc is capable of directly linking to other rustdoc pages in Markdown documentation using the path of item as a link.
3
+
Rustdoc is capable of directly linking to other rustdoc pages using the path of
4
+
the item as a link.
4
5
5
6
For example, in the following code all of the links will link to the rustdoc page for `Bar`:
6
7
@@ -19,15 +20,26 @@ pub struct Foo3;
19
20
/// This struct is also not [`Bar`]
20
21
pubstructFoo4;
21
22
23
+
/// This struct *is* [`Bar`]!
22
24
pubstructBar;
23
25
```
24
26
25
-
You can refer to anything in scope, and use paths, including `Self`, `self`, `super`, and `crate`. You may also use `foo()` and `foo!()` to refer to methods/functions and macros respectively. Backticks around the link will be stripped.
27
+
Backticks around the link will be stripped, so ``[`Option`]`` will correctly
28
+
link to `Option`.
29
+
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.
32
+
33
+
You can also refer to items with generic parameters like `Vec<T>`. The link will
34
+
resolve as if you had written ``[`Vec<T>`](Vec)``. Fully-qualified syntax (for example,
35
+
`<Vec as IntoIterator>::into_iter()`) is [not yet supported][fqs-issue], however.
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@`, `prim@`, `primitive@`, `macro@`, or `derive@`:
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@`:
54
68
55
69
```rust
56
70
/// See also: [`Foo`](struct@Foo)
@@ -62,4 +76,19 @@ struct Foo {}
62
76
fnFoo() {}
63
77
```
64
78
65
-
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.
79
+
You can also disambiguate for functions by adding `()` after the function name,
80
+
or for macros by adding `!` after the macro name:
81
+
82
+
```rust
83
+
/// See also: [`Foo`](struct@Foo)
84
+
structBar;
85
+
86
+
/// This is different from [`Foo()`]
87
+
structFoo {}
88
+
89
+
fnFoo() {}
90
+
```
91
+
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.
0 commit comments