# Linking examples within rustdoc

**URL:** <https://internals.rust-lang.org/t/linking-examples-within-rustdoc/13615>\
**Category:** documentation\
**Created:** [December 17, 2020, 11:17pm UTC](https://internals.rust-lang.org/t/linking-examples-within-rustdoc/13615 "2020-12-17T23:17:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![willcrichton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/willcrichton/32/7462_2.png) [@willcrichton](https://internals.rust-lang.org/u/willcrichton)\
**Post date:** [December 17, 2020, 11:17pm UTC](https://internals.rust-lang.org/t/linking-examples-within-rustdoc/13615/1 "2020-12-17T23:17:58Z")

</div>

# The Idea

[JD](https://github.com/zamfi) and I have been working on a rustdoc extension that automatically links methods to method calls in the `examples/` directory of a crate. The goal is to make it easier for people reading documentation to find examples of how to use a method.

Here's what that looks like for the [`warp::Filter::and`](https://docs.rs/warp/0.2.5/warp/trait.Filter.html#method.and) method.

 ![Screen Shot 2020-12-17 at 6.03.24 PM](https://us1.discourse-cdn.com/flex002/uploads/rustlang/original/2X/5/5aa9797c6c40e199a70440cccdd67c9a52a21dd9.png)

By default, the UI shows a single example. The user can click "More examples" to see every instance.

 ![Screen Shot 2020-12-17 at 6.03.36 PM](https://us1.discourse-cdn.com/flex002/uploads/rustlang/original/2X/4/4061783842baa5e175acab5368d76cc5063eca82.png)

The headings, e.g. `examples/returning.rs` are links to the corresponding Github page for the example.

# How it works

Check out the [`example-analyzer`](https://github.com/willcrichton/example-analyzer) repo and my [rust fork](https://github.com/willcrichton/rust) for details. In short, we wrote a script that uses the rustc interface to type check every script in the `examples/` directory. We dump a JSON file of every function call mapped to `(File, Vec<Span>)` pairs. Then rustdoc reads the JSON file and outputs these snippets for every method with invocations in `examples/`.

# Desired feedback

We're posting this here because we eventually want to merge this feature into rustdoc. We'd like to get feedback on the UI (better ways to show the examples?) and on the workflow (how to eventually get this integrated with cargo?). Here are a few concrete technical problems we encountered.

1. To be able to type-check the examples, we needed a rustc invocation that included all necessary flags like extern libs. With the deprecation of `cargo build-plan`, we have to [hackily scrape that info](https://github.com/willcrichton/example-analyzer/blob/2099ae837889f55f35f323722986ce1269d1c491/src/main.rs#L132-L157) from stderr by running `cargo check`. What's a better way to do this?
2. How do we know where to look for examples? Right now we run `cargo check --examples` to determine which files are examples, is that good enough?
3. We have to serialize an identifier for methods that can be passed between the analysis and rustdoc phases. Right now we use a [particular way](https://github.com/willcrichton/example-analyzer/blob/2099ae837889f55f35f323722986ce1269d1c491/src/main.rs#L56-L60) of getting a string name of each method, but is there a more robust means of doing this? eg serializing (DefId, CrateNum) pairs or something.

Any other feedback is welcome!

---

<div class="post-metadata">

**Author:** ![jyn514](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jyn514/32/13245_2.png) [@jyn514](https://internals.rust-lang.org/u/jyn514)\
**Post date:** [December 18, 2020, 3:26am UTC](https://internals.rust-lang.org/t/linking-examples-within-rustdoc/13615/2 "2020-12-18T03:26:47Z")

</div>

A lot of these answers depend if you want to keep this as an independent tool or not. I've assumed that you want to merge this into rustdoc and aren't interested in keeping the separate tool.

> - To be able to type-check the examples, we needed a rustc invocation that included all necessary flags like extern libs. With the deprecation of `cargo build-plan` , we have to [hackily scrape that info](https://github.com/willcrichton/example-analyzer/blob/2099ae837889f55f35f323722986ce1269d1c491/src/main.rs#L132-L157) from stderr by running `cargo check` . What's a better way to do this?

You should get this 'for free' by running under cargo. You can test today with `RUSTC=example-analyzer cargo build --examples`.

The hard thing is actually not getting the extern flags, it's convincing cargo that the examples need to be built before documenting the crate and that building an example has outputs that need to be passed as inputs to `cargo doc`.

> - How do we know where to look for examples? Right now we run `cargo check --examples` to determine which files are examples, is that good enough?

I would expect Cargo itself to pass this information with `--examples=/path/to/example` - I don't think it makes sense for cargo to call rustdoc and then rustdoc to again call cargo.

> - We have to serialize an identifier for methods that can be passed between the analysis and rustdoc phases. Right now we use a [particular way](https://github.com/willcrichton/example-analyzer/blob/2099ae837889f55f35f323722986ce1269d1c491/src/main.rs#L56-L60) of getting a string name of each method, but is there a more robust means of doing this? eg serializing (DefId, CrateNum) pairs or something.

I think serializing a `DefId` would be more robust, but IIRC we tried this last week and it didn't work. I would either ask on Zulip the recommended way to do this or we might even need to ask for a way to serialize custom metadata depending on the command 😕

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [December 18, 2020, 9:28am UTC](https://internals.rust-lang.org/t/linking-examples-within-rustdoc/13615/3 "2020-12-18T09:28:45Z")

</div>

> [@jyn514](#):
>
> I think serializing a `DefId` would be more robust, but IIRC we tried this last week and it didn't work. I would either ask on Zulip the recommended way to do this or we might even need to ask for a way to serialize custom metadata depending on the command

Can you serialize `tcx.crate_hash(def_id.krate)` combined with the `tcx.def_path(*def_id).to_string_no_crate_verbose()` you are already serializing? `CrateNum` is a number referencing a crate with a specific SVH (`tcx.crate_hash`). This mapping is different for each compilation session.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 18, 2021, 9:28am UTC](https://internals.rust-lang.org/t/linking-examples-within-rustdoc/13615/4 "2021-03-18T09:28:56Z")

</div>

Closed on March 18, 2021, 9:28am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
