我正在建一个锈蚀图书馆,想给它打磨一下。在rustdoc中,我有时希望链接到文档中的库的其他部分,例如fns、traits或structs。这方面的官方语法是什么?
发布于 2016-08-17 09:56:02
从Rust 1.48开始,Rustdoc现在支持直接的文档内部链接。
预锈蚀1.48:
Rustdoc似乎为一个机箱的组成元素生成了大部分确定性的文件名。因此,如果您有一个名为enum的Complex,通常可以使用以下方法链接到它:
[Complex](enum.Complex.html)类似地,名为struct的Point类似于:
[Point](struct.Point.html)这应该扩展到大多数定义(fn、trait等)。
为了在不同的嵌套级别引用机箱的元素,可以使用相对路径(其中每个模块都是自己的文件夹):
[Point](../model/struct.Point.html)或使用绝对路径:
[Point](/crate_name/model/struct.Point.html)如果构建文档(cargo doc --no-deps --open)并导航到它们想要的字段或项,并注意到URL,则可以推导出更多的这些“约定”,包括特定字段的锚等等。请记住,只有发布项目才会发布到文档中。
发布于 2018-11-27 16:39:47
从锈1.48开始,您现在可以依赖RFC 1946年了。这就增加了文档内部链接的概念。这允许使用锈蚀路径作为链接的URL:
[Iterator](std::iter::Iterator)[Iterator][iter],以及文档中的其他地方:[iter]: std::iter::Iterator[Iterator],以及文档中的其他地方:[Iterator]: std::iter::IteratorRFC还引入了“隐含快捷参考链接”。这允许省略链接引用,然后自动推断链接引用。
[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>发布于 2018-11-27 14:01:16
如果要链接结构的某些特定部分,例如,在同一个结构中使用名为foo的方法(使用稳定锈蚀,而不是夜间使用)
[foo](#method.foo)或者如果它在另一个结构中
[foo](struct.OtherStruct.html#method.foo)https://stackoverflow.com/questions/31582064
复制相似问题