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

2024-03-13

我正在构建一个 Rust 库,并想对其进行一些改进。在 rustdoc 中,我有时想link文档中库的其他部分,例如fns, traits or structs。官方语法是什么?


As of 铁锈 1.48 https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1480-2020-11-19,您现在可以信赖RFC 1946 https://rust-lang.github.io/rfcs/1946-intra-rustdoc-links.html。这增加了文档内链接的概念。这允许使用锈迹路径 https://doc.rust-lang.org/reference/paths.html作为链接的 URL:

  1. [Iterator](std::iter::Iterator)
  2. [Iterator][iter],以及文档中的其他位置:[iter]: std::iter::Iterator
  3. [Iterator],以及文档中的其他位置:[Iterator]: std::iter::Iterator

RFC 还介绍了“隐含的快捷方式参考链接” https://rust-lang.github.io/rfcs/1946-intra-rustdoc-links.html#implied-shortcut-reference-links。这允许省略链接引用,然后自动推断该链接引用。

  1. [std::iter::Iterator],在文档中的其他任何地方都没有迭代器的链接引用定义
  2. [`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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐