How to include an arbitrary markdown file as a documentation attribute? [duplicate]
Asked Answered
B

1

8

If the readme Cargo.toml key is set, doc.rs renders the README on the crate's index page. Is there a way to emulate this when running cargo doc locally?

If I add:

#![doc = r###"contents
of
README.md
here
"###]

as a literal, I get the behaviour I'm looking for, but inlining a copy of my whole README.md is pretty inconvenient for making updates.

I tried:

#![doc = include!("README.md")]

but this gives an error:

error: unexpected token: `include`
 --> src/lib.rs:3:10
  |
3 | #![doc = include!("README.md")]
  |          ^^^^^^^
Bearnard answered 8/8, 2019 at 22:36 Comment(0)
B
12

Update as of Rust 1.54.0:

#![doc = include_str!("path/to/docs.md")]

Original answer:

There is an unstable feature, external-doc, which enables this:

Example usage (nightly-only):

#![feature(external_doc)]

#![doc(include = "../README.md")]
Bearnard answered 8/8, 2019 at 22:45 Comment(1)
#[doc = include_str!("my_doc.md") is the new way to do this in nightly via #78837Influx

© 2022 - 2024 — McMap. All rights reserved.