[英]How to link to other fns/structs/enums/traits in rustdoc?
我正在构建一个 Rust 库并想对其进行一些润色。 在 rustdoc 中,我有时想链接到文档中库的其他部分,例如fn
s、 trait
s 或struct
s。 这个的官方语法是什么?
从Rust 1.48 开始,您现在可以依赖RFC 1946 。 这增加了文档内链接的概念。 这允许使用Rust 路径作为链接的 URL:
[Iterator](std::iter::Iterator)
[Iterator][iter]
,以及文档中的其他地方:[iter]: std::iter::Iterator
[Iterator]
,以及文档中的其他地方:[Iterator]: std::iter::Iterator
RFC 还引入了“隐含的快捷方式参考链接” 。 这允许省略链接引用,然后自动推断。
[std::iter::Iterator]
,在文档中的其他任何地方都没有[std::iter::Iterator]
的链接引用定义[`std::iter::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,
}
}
生成此文档:
具体来说,这个 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>
从 Rust 1.48 开始,Rustdoc 现在支持直接的内部文档链接。
Rust 1.48 前:
Rustdoc
似乎主要为 crate 的组成元素生成确定性的文件名。 因此,如果您有一个名为Complex
的enum
您通常可以使用以下方法链接到它:
[Complex](enum.Complex.html)
类似地,一个名为Point
的struct
看起来像:
[Point](struct.Point.html)
这应该适用于大多数定义( fn
、 trait
等)。
要在不同的嵌套级别引用 crate 的元素,您可以使用相对路径(其中每个模块都是自己的文件夹):
[Point](../model/struct.Point.html)
或使用绝对路径:
[Point](/crate_name/model/struct.Point.html)
如果构建文档( cargo doc --no-deps --open
)并导航到他们想要的字段或项目并记下 URL,则可以推断出更多这些“约定”,包括特定字段的锚点等. 请记住,只有发布项目才会发布到文档。
如果想要链接结构的某些特定部分,例如,同一结构中名为foo
的方法(使用稳定的rust,而不是nightly )
[foo](#method.foo)
或者如果它在另一个结构中
[foo](struct.OtherStruct.html#method.foo)
在 Rust 1.49 nightly 中它可以工作(1.48 稳定版尚未发布):
super::structs::WebApiResponse
]mycrate::structs::WebApiResponse
]等等。
由于文档是用 Markdown 编写的,因此只需使用超链接的 Markdown 语法; IE
[anchor text](URL)
另外,看看这个: https : //doc.rust-lang.org/book/documentation.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.