3

I run code using stable and document it using nightly, mostly because of the automatic links.

I tried to include a long inner documentation by #![doc = include_str!("doc.md")] in the code, but only use it if using nightly, as it is unstable. The standard way to do it seems to be with an explicit feature.

I declared a nightly feature on my Cargo.toml, and tried:

#![cfg_attr(feature = "nightly", 
    feature(extended_key_value_attributes),
    doc = include_str!("doc.md"))]

which works as expected with cargo +nightly doc --features nightly, but is not supported by stable:

> cargo +stable build
[...]
error[E0658]: arbitrary expressions in key-value attributes are unstable

I suspect the cfg_attr has to parse all its arguments even if not using them, so the macro syntax within the #[]makes things more complicated.

How can I do this correctly?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Shadok
  • 144
  • 5
  • *mostly because of the automatic links* — those are stable as of Rust 1.48. – Shepmaster Jun 01 '21 at 15:16
  • 2
    Try moving the code that uses the feature into a separate file / module and add the `cfg` statement to the `mod`. Hopefully, that way the file isn't even parsed. – Shepmaster Jun 01 '21 at 15:19
  • 1
    See also [How is it possible to keep Rust module documentation in separate Markdown files?](https://stackoverflow.com/a/57421673/155423) for how to do the behavior you want even in stable. – Shepmaster Jun 01 '21 at 15:20

0 Answers0