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

时间:2015-07-23 08:30:30

标签: rust rustdoc

我正在构建一个Rust库并希望给它一些润色。在rustdoc中,我有时希望链接到文档中的库的其他部分,例如fn s,traitstruct s。这是什么官方语法?

5 个答案:

答案 0 :(得分:20)

Rustdoc似乎为箱子的组成元素生成大多数确定性的文件名。因此,如果您有一个名为enum的{​​{1}},您通常可以使用以下链接链接到它:

Complex

类似地,名为[Complex](enum.Complex.html) 的{​​{1}}看起来像:

struct

这应该延续到大多数定义(Point[Point](struct.Point.html) 等等。

我应该注意,在某些情况下,可能无效。如果由于某种原因,fn生成的HTML文件最终处于不同的嵌套级别,我上面列出的相对链接可能会trait。我还没有遇到这个问题。

答案 1 :(得分:14)

RFC 1946添加了文档内链接的概念。这允许使用Rust paths作为链接的URL:

  
[Iterator](std::iter::Iterator)
[Iterator][iter], and somewhere else in the document: [iter]: std::iter::Iterator
[Iterator], and somewhere else in the document: [Iterator]: std::iter::Iterator

此功能尚未稳定,但是如果您使用夜间工具链来构建文档,则会自动启用。


作为一个具体示例,此源代码:

//! 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,
    }
}

制作此文档:

example generated documentation

具体来说,将生成以下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>

答案 2 :(得分:2)

由于文档是用Markdown编写的,因此只需使用Markdown语法进行超链接;即。

[anchor text](URL)

另外,请看一下:https://doc.rust-lang.org/book/documentation.html

答案 3 :(得分:1)

如果要链接结构的某些特定部分,例如,同一结构中的方法

[from_file](#method.from_file)

或者如果它在另一个结构中

[from_file](struct.SafeAccount.html#method.from_file)

答案 4 :(得分:0)

在Rust 每晚1.49 中有效(1.48稳定版尚未发布):

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

Read here