Pick which of several methods with the same name to refer to in rustdoc

Suppose I have following code:

pub struct A<T>(T);

impl A<u8> {
    /// See also [`A::inc`]
    pub fn inc(&mut self) {
        self.0 += 1;
    }
}

impl A<u16> {
    /// See also [`A::inc`]
    pub fn inc(&mut self) {
        self.0 += 1;
    }
}

Both of the [`A::inc`] references point to the same method. How do I make a reference to a specific method?

Maybe you can provide the generic parameter like A::<u16>::inc for example to make the docs reference the inc implementation of A<u16>? I'm on mobile and can't test myself if that's the right syntax.

Nope, that's the first thing I tried, along with placing them into separate modules.

Since the second inc generates struct.A.html#method.inc-1 in the link, so you can use [`A::<u16>::inc`](#method.inc-1) to refer to it.

Update: or this by tagging an anchor if you're not sure about the enumeration

impl A<u16> {
    /// <a name="A-u16-inc"></a>
    ///
    /// See also [`A::<u16>::inc`](#A-u16-inc)
    pub fn inc(&mut self) {
        self.0 += 1;
    }
}

Well... Yea, this works too, but I was hoping for something that works better across multiple modules and would complain if something is missing/renamed...

If your desired anchor doesn't appear in the same webpage, it seems rustdoc supports relative path in the link

impl A<u16> {
    /// <a name="A-u16-inc"></a>
    pub fn inc(&mut self) {
        self.0 += 1;
    }
}
pub mod a {
    /// See [`A::<u16>::inc`](../struct.A.html#A-u16-inc) -> This correctly leads to its place.
    pub fn f() {}
}

Update: ../struct.A.html can be replaced with Rust's path syntax, i.e. crate::A and super::A etc due to 1946-intra-rustdoc-links - The Rust RFC Book .

Very fragile :slight_smile: I guess I should check tickets on rustdoc on this subject and make one if there's none.

Not hard to find

2 Likes