rustrustdoc

How to link to other fns/structs/enums/traits in rustdoc?


I'm building a Rust library and want to give it some polish. In the rustdoc, I'd sometimes like to link to other parts of the library within the docs, e.g. fns, traits or structs. What is the official syntax for this?


Solution

  • As of Rust 1.48, Rustdoc now supports direct intra-doc links.


    Pre Rust 1.48:

    Rustdoc seems to generate mostly deterministic filenames for constituent elements of a crate. Therefore if you have an enum named Complex you can generally link to it using:

    [Complex](enum.Complex.html)
    

    Similarly a struct called Point would look like:

    [Point](struct.Point.html)
    

    This should carry over to most definitions (fn, trait, and so on).

    For referencing elements of a crate at different nesting levels, you can use relative paths (where each module is its own folder):

    [Point](../model/struct.Point.html)
    

    or use absolute paths:

    [Point](/crate_name/model/struct.Point.html)
    

    More of these "conventions", including anchors for specific fields, etc., can be deduced if one builds docs (cargo doc --no-deps --open) and navigates to the field or item they want and takes note of the URL. Remember that only pub items are published to docs.