我正在构建一个Rust库并想要它更加完善。在 rustdoc 中,我有时想要在文档中链接到库中的其他部分,例如 fn
、trait
或 struct
。这个官方的语法是什么?
我正在构建一个Rust库并想要它更加完善。在 rustdoc 中,我有时想要在文档中链接到库中的其他部分,例如 fn
、trait
或 struct
。这个官方的语法是什么?
[Iterator](std::iter::Iterator)
[Iterator][iter]
,并且在文档的其他地方:[iter]: std::iter::Iterator
[Iterator]
,并且在文档的其他地方:[Iterator]: std::iter::Iterator
[std::iter::Iterator]
,没有在文档的任何其他地方定义Iterator的链接引用[`std::iter::Iterator`]
,没有在文档的任何其他地方定义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,
}
}
生成此文档:
具体而言,生成了以下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>
[foo](crate::stuff::thing)
似乎可行。 - rodrigocfd[my_lib_crate::module::Symbol]
会警告链接已损坏;尝试[../my_lib_crate::module::Symbol]
则无法识别此链接。有什么方法可以实现这一点吗? - Oren Ben-Kiki从 Rust 1.48 开始,Rustdoc 现在支持直接的文档内链接。
Rust 1.48 之前:
Rustdoc
似乎为一个 crate 的组成元素生成大多数确定性的文件名。因此,如果您有一个名为Complex
的 enum
,通常可以使用以下方式链接到它:
[Complex](enum.Complex.html)
同样地,一个名为Point
的struct
看起来像这样:
[Point](struct.Point.html)
这应该适用于大多数定义(fn
,trait
等)。
要引用嵌套级别不同的箱中的元素,可以使用相对路径(其中每个模块都是自己的文件夹):
[Point](../model/struct.Point.html)
或者使用绝对路径:
[Point](/crate_name/model/struct.Point.html)
如果一个人建立文档 (cargo doc --no-deps --open
) 并导航到他们想要的字段或项并注意 URL,就可以推断出更多关于这些“约定”的信息,包括特定字段的锚点等。请记住,只有 pub 项发布到文档中。
[Point](/crate_name/model/struct.Point.html)
看起来已经不能用了,至少当我在我的文件系统上打开文档时是这样的。它链接到 file:///crate_name/...
。 - cyclaministfoo
的方法(使用稳定版的 Rust,而非夜间版)。[foo](#method.foo)
或者如果它在另一个结构体中
[foo](struct.OtherStruct.html#method.foo)
super::structs::WebApiResponse
]mycrate::structs::WebApiResponse
]等等。
由于文档是用Markdown编写的,因此只需使用Markdown语法来创建超链接;即
[anchor text](URL)