简体   繁体   中英

rustdoc link to enum variant

In rust, I want rustdoc text to link to an enum variant. What is the syntax for that?

Example

Given rust code residing at project file src/common.rs

/// 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() {}

The rust docs are built with command:

cargo doc --locked --release --frozen --no-deps -v

Problem

In the generated rust docs, the various link anchors fail to link to the enum variants within MyResult .

The created doc looks like:

Returns true if the result is [Found], [Done].

In other words, this is not an Err
  • The text [Found] , and [Done] fail to link.
  • The text Err links to https://doc.rust-lang.org/beta/core/result/enum.Result.html#variant.Err .
  • I have also tried other variations of linking syntax like
    • /// [Done]: MyResult#variant.Done
    • /// [Done]: self::MyResult#variant.Done


How do I create rust doc intra-doc links to in-module enum variants?

Use syntax form /// [SomeVariant]: self::MyEnum#variant.SomeVariant .

The question's example rustdoc code should look like:

/// 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() {}

In the rustdoc output, clicking on the text Found then jumps to the definition for the enum variant Found .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM