|
| 1 | +# Links |
| 2 | + |
| 3 | +This chapter explains how links should be handled by the Reference. Several of these capabilities are provided by [`mdbook-spec`](tooling/mdbook-spec.md). |
| 4 | + |
| 5 | +See also the [linkchecker tests](tests.md#linkcheck) for testing links. |
| 6 | + |
| 7 | +## Standard library links |
| 8 | + |
| 9 | +You should link to the standard library without specifying a URL in a fashion similar to [rustdoc intra-doc links][intra]. Some examples: |
| 10 | + |
| 11 | +We can link to the page on `Option`: |
| 12 | + |
| 13 | +```markdown |
| 14 | +[`std::option::Option`] |
| 15 | +``` |
| 16 | + |
| 17 | +In these links, generics are ignored and can be included: |
| 18 | + |
| 19 | +```markdown |
| 20 | +[`std::option::Option<T>`] |
| 21 | +``` |
| 22 | + |
| 23 | +If we don't want the full path in the text, we can write: |
| 24 | + |
| 25 | +```markdown |
| 26 | +[`Option`](std::option::Option) |
| 27 | +``` |
| 28 | + |
| 29 | +Macros can end in `!`. This can be helpful for disambiguation. For example, this refers to the macro rather than the module: |
| 30 | + |
| 31 | +```markdown |
| 32 | +[`alloc::vec!`] |
| 33 | +``` |
| 34 | + |
| 35 | +Explicit namespace disambiguation is also supported: |
| 36 | + |
| 37 | +```markdown |
| 38 | +[`std::vec`](mod@std::vec) |
| 39 | +``` |
| 40 | + |
| 41 | +Beware there are some limitations, for example: |
| 42 | + |
| 43 | +- Links to rexports from `std_arch` don't work due to <https://github.com/rust-lang/rust/issues/96506>. |
| 44 | +- Links to keywords aren't supported. |
| 45 | +- Links to trait impls where the trait is not in the prelude doesn't work. Traits must be in scope, and there currently isn't a way to add those. |
| 46 | +- If there are multiple generic implementations, it will link to one randomly (see <https://github.com/rust-lang/rust/issues/76895>). |
| 47 | + |
| 48 | +When running into a rustdoc limitation, consider manually linking to the correct page using a relative link. For example, `../std/arch/macro.is_x86_feature_detected.html`. |
| 49 | + |
| 50 | +When rendering the Reference locally, by default it uses relative links to conform with how the books are published. This probably isn't what you want, so you usually will want to set the [`SPEC_RELATIVE=0` environment variable][rel] so that the links go to the live site instead. |
| 51 | + |
| 52 | +[intra]: https://doc.rust-lang.org/rustdoc/write-documentation/linking-to-items-by-name.html |
| 53 | +[rel]: tooling/building.md#spec_relative |
0 commit comments