在 rust 中,我希望 rustdoc 文本链接到枚举变体。其语法是什么?
示例
给定rust code驻留在项目文件 src/common.rs
中,(此 rustdoc 代码无法链接)
/// your result!
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum MyResult<T, E> {
/// this is good!
Found(T),
/// task completed!
Done,
/// this is bad!
Err(E),
}
impl<T, E> MyResult<T, E> {
/// Returns `true` if the result is [`Found`], [`Done`].
///
/// In other words, this is not an [`Err`](Err)
///
/// [Found]: self::MyResult::Found
/// [Done]: self::Done
/// [Err]: crate::common::MyResult::Err
pub const fn is_ok(&self) -> bool {
matches!(*self, MyResult::Found(_) | MyResult::Done)
}
}
fn main() {}
Rust 文档是使用以下命令构建的:
cargo doc --locked --release --frozen --no-deps -v
问题
在生成的 rust 文档中,各种链接 anchor 无法链接到 MyResult
中的枚举变体。
创建的文档如下所示:
Returns true if the result is [Found], [Done].
In other words, this is not an Err
- 文本
[Found]
和[Done]
无法链接。 - 文本
Err
链接到https://doc.rust-lang.org/beta/core/result/enum.Result.html#variant.Err
。 - 我还尝试了链接语法的其他变体,例如
///[完成]: MyResult#variant.Done
///[完成]: self::MyResult#variant.Done
如何创建指向模块内 enum
变体的 rust 文档内文档链接?
最佳答案
使用语法形式
/// [SomeVariant]: self::MyEnum#variant.SomeVariant
问题的示例 rustdoc 代码将具有 #variant.
链接 anchor :
/// your result!
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum MyResult<T, E> {
/// this is good!
Found(T),
/// task completed!
Done,
/// this is bad!
Err(E),
}
impl<T, E> MyResult<T, E> {
/// Returns `true` if the result is [`Found`], [`Done`].
///
/// In other words, this is not an [`Err`]
///
/// [`Found`]: self::MyResult#variant.Found
/// [`Done`]: self::MyResult#variant.Done
/// [`Err`]: self::MyResult#variant.Err
pub const fn is_ok(&self) -> bool {
matches!(*self, MyResult::Found(_) | MyResult::Done)
}
}
fn main() {}
在 rustdoc 输出中,单击文本 Found
,然后跳转到枚举变体 Found
的定义。
相关,链接方法类似,使用#method.
链接 anchor :
/// [`MyStruct.my_method()`]: self::MyStruct#method.my_method
关于rustdoc 链接到枚举变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73316074/