@@ -212,6 +212,36 @@ pub struct BigX;
212212Then, when looking for it through the ` rustdoc ` search, if you enter "x" or
213213"big", search will show the ` BigX ` struct first.
214214
215+ ### Include items only when collecting doctests
216+
217+ Rustdoc's [ documentation tests] can do some things that regular unit tests can't, so it can
218+ sometimes be useful to extend your doctests with samples that wouldn't otherwise need to be in
219+ documentation. To this end, Rustdoc allows you to have certain items only appear when it's
220+ collecting doctests, so you can utilize doctest functionality without forcing the test to appear in
221+ docs, or to find an arbitrary private item to include it on.
222+
223+ If you add ` #![feature(cfg_doctest)] ` to your crate, Rustdoc will set ` cfg(doctest) ` when collecting
224+ doctests. Note that they will still link against only the public items of your crate; if you need to
225+ test private items, unit tests are still the way to go.
226+
227+ In this example, we're adding doctests that we know won't compile, to verify that our struct can
228+ only take in valid data:
229+
230+ ``` rust
231+ #![feature(cfg_doctest)]
232+
233+ /// We have a struct here. Remember it doesn't accept negative numbers!
234+ pub struct MyStruct (usize );
235+
236+ /// ```compile_fail
237+ /// let x = my_crate::MyStruct(-5);
238+ /// ```
239+ #[cfg(doctest)]
240+ pub struct MyStructOnlyTakesUsize ;
241+ ```
242+
243+ [ documentation tests ] : documentation-tests.html
244+
215245## Unstable command-line arguments
216246
217247These features are enabled by passing a command-line flag to Rustdoc, but the flags in question are
0 commit comments