0

Is there a way to include something into documentation scope without including it into the code scope? My Rustdoc links to a Trait that's not in the code scope. If I add use std::ops::Add at the top, I get unused import warning from cargo build. If I don't add it, I get unresolved link warning from cargo doc.

In theory, cargo build should not warn me because rustdoc is in the same scope as the main code, but it does.

/// [Add]
pub fn foo() {}
Ken White
  • 123,280
  • 14
  • 225
  • 444
Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97

2 Answers2

1

You can fully qualify the link.

/// [Add](std::ops::Add)

Note that the syntax you've been using is shorthand for

/// [Add](Add)

and the thing in parentheses at the end can be any valid qualified name, including fully qualified ones starting at std or the name of some other crate.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
1

You can use #[cfg(doc)]:

#[cfg(doc)]
use std::ops::Add;

/// [Add]
pub fn foo() {}

Or just use the full name, as suggested by @SilvioMayolo.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77