Reputation: 7144
In rust, I want rustdoc text to link to an enum variant. What is the syntax for that?
Given rust code residing at project file src/common.rs
, (this rustdoc code failed to link)
/// 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
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
[Found]
, and [Done]
fail to link.Err
links to https://doc.rust-lang.org/beta/core/result/enum.Result.html#variant.Err
./// [Done]: MyResult#variant.Done
/// [Done]: self::MyResult#variant.Done
How do I create rust doc intra-doc links to in-module enum
variants?
Upvotes: 2
Views: 989
Reputation: 7144
Use syntax form
/// [SomeVariant]: self::MyEnum#variant.SomeVariant
The question's example rustdoc code will have #variant.
link 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() {}
In the rustdoc output, clicking on the text Found
then jumps to the definition for the enum variant Found
.
Related, linking to methods is similar, use #method.
link anchor:
/// [`MyStruct.my_method()`]: self::MyStruct#method.my_method
Upvotes: 3