简体   繁体   English

如何链接到 rustdoc 中的其他 fns/structs/enums/traits?

[英]How to link to other fns/structs/enums/traits in rustdoc?

I'm building a Rust library and want to give it some polish.我正在构建一个 Rust 库并想对其进行一些润色。 In the rustdoc, I'd sometimes like to link to other parts of the library within the docs, eg fn s, trait s or struct s.在 rustdoc 中,我有时想链接到文档中库的其他部分,例如fn s、 trait s 或struct s。 What is the official syntax for this?这个的官方语法是什么?

As of Rust 1.48 , you can now rely on RFC 1946 .Rust 1.48 开始,您现在可以依赖RFC 1946 This adds the concept of intra-documentation links.这增加了文档内链接的概念。 This allows using Rust paths as the URL of a link:这允许使用Rust 路径作为链接的 URL:

  1. [Iterator](std::iter::Iterator)
  2. [Iterator][iter] , and somewhere else in the document: [iter]: std::iter::Iterator [Iterator][iter] ,以及文档中的其他地方: [iter]: std::iter::Iterator
  3. [Iterator] , and somewhere else in the document: [Iterator]: std::iter::Iterator [Iterator] ,以及文档中的其他地方: [Iterator]: std::iter::Iterator

The RFC also introduces "Implied Shortcut Reference Links" . RFC 还引入了“隐含的快捷方式参考链接” This allows leaving out the link reference, which is then inferred automatically.这允许省略链接引用,然后自动推断。

  1. [std::iter::Iterator] , without having a link reference definition for Iterator anywhere else in the document [std::iter::Iterator] ,在文档中的其他任何地方都没有[std::iter::Iterator]的链接引用定义
  2. [`std::iter::Iterator`] , without having a link reference definition for Iterator anywhere else in the document (same as previous style but with back ticks to format link as inline code) [`std::iter::Iterator`] ,在文档中的其他任何地方都没有[`std::iter::Iterator`]的链接引用定义(与以前的样式相同,但带有反勾以将链接格式化为内联代码)

As a concrete example, this source code:作为一个具体的例子,这个源代码:

//! Check out [ExampleStruct], especially [this
//! method](ExampleStruct::foo), but [the trait method][trait] is also
//! cool. There is also [an enum variant you can
//! use](nested::ExampleEnum::Beta).
//!
//! [trait]: ExampleTrait::bar

pub struct ExampleStruct;

impl ExampleStruct {
    pub fn foo(&self) {}
}

pub trait ExampleTrait {
    fn bar();
}

pub mod nested {
    pub enum ExampleEnum {
        Alpha,
        Beta,
    }
}

Produces this documentation:生成此文档:

示例生成的文档

Specifically, this HTML is generated:具体来说,这个 HTML 是生成的:

<p>Check out <a href="../doc_link_example/struct.ExampleStruct.html" title="ExampleStruct">ExampleStruct</a>, especially <a href="../doc_link_example/struct.ExampleStruct.html#method.foo">this method</a>, but <a href="../doc_link_example/trait.ExampleTrait.html#tymethod.bar">the trait method</a> is also cool. There is also <a href="../doc_link_example/nested/enum.ExampleEnum.html#Beta.v">an enum variant you can use</a>.</p>

As of Rust 1.48, Rustdoc now supports direct intra-doc links.从 Rust 1.48 开始,Rustdoc 现在支持直接的内部文档链接。


Pre Rust 1.48: Rust 1.48 前:

Rustdoc seems to generate mostly deterministic filenames for constituent elements of a crate. Rustdoc似乎主要为 crate 的组成元素生成确定性的文件名。 Therefore if you have an enum named Complex you can generally link to it using:因此,如果您有一个名为Complexenum您通常可以使用以下方法链接到它:

[Complex](enum.Complex.html)

Similarly a struct called Point would look like:类似地,一个名为Pointstruct看起来像:

[Point](struct.Point.html)

This should carry over to most definitions ( fn , trait , and so on).这应该适用于大多数定义( fntrait等)。

For referencing elements of a crate at different nesting levels, you can use relative paths (where each module is its own folder):要在不同的嵌套级别引用 crate 的元素,您可以使用相对路径(其中每个模块都是自己的文件夹):

[Point](../model/struct.Point.html)

or use absolute paths:或使用绝对路径:

[Point](/crate_name/model/struct.Point.html)

More of these "conventions", including anchors for specific fields, etc., can be deduced if one builds docs ( cargo doc --no-deps --open ) and navigates to the field or item they want and takes note of the URL.如果构建文档( cargo doc --no-deps --open )并导航到他们想要的字段或项目并记下 URL,则可以推断出更多这些“约定”,包括特定字段的锚点等. Remember that only pub items are published to docs.请记住,只有发布项目才会发布到文档。

If one wants to link some specific part of a struct eg, a method named foo in the same struct (using stable rust, not nightly )如果想要链接结构的某些特定部分,例如,同一结构中名为foo的方法(使用稳定的rust,而不是nightly

[foo](#method.foo)

or if it is in another struct或者如果它在另一个结构中

[foo](struct.OtherStruct.html#method.foo)

In Rust 1.49 nightly it works (1.48 stable not released yet):在 Rust 1.49 nightly 中它可以工作(1.48 稳定版尚未发布):

  • [ super::structs::WebApiResponse ] [ super::structs::WebApiResponse ]
  • [ mycrate::structs::WebApiResponse ] [ mycrate::structs::WebApiResponse ]

etc.等等。

Read here 在这里阅读

Since the documentation is written in Markdown, just use the Markdown syntax for Hyperlinks;由于文档是用 Markdown 编写的,因此只需使用超链接的 Markdown 语法; ie IE

[anchor text](URL)

Also, take a look at this: https://doc.rust-lang.org/book/documentation.html另外,看看这个: https : //doc.rust-lang.org/book/documentation.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM