rustdoc 链接到枚举变体

标签 rust enums rustdoc

在 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/

相关文章:

ssh - 将 SSH 命令连接到标准输出

rust - 如何取得任何 :downcast_ref from trait object? 的所有权

enums - 类型常量声明列表

rust - 你如何处理 "could not parse code block as Rust code"rustdoc 警告?

rust - 如何为 Rust : cargo run? 创建快捷键

rust - 在 Rust 中更改终端光标位置

sql - SQL中ENUM列的大小?

c - 在 [C] 中将 `enum` 转换为其他 `enum` 是否有效?

rust - 在文档中将长表行分成多行

rust - 如何为私有(private)元素生成文档