about summary refs log tree commit diff
path: root/src/doc
diff options
context:
space:
mode:
authorQuietMisdreavus <grey@quietmisdreavus.net>2019-06-28 10:31:27 -0500
committerQuietMisdreavus <grey@quietmisdreavus.net>2019-07-06 21:37:17 -0500
commitbed54cf85457d76f479fcd406f07b39c5e8abb12 (patch)
tree6f57cc26d9d118bbe1b59afd1e08cebe9659c126 /src/doc
parentb0bd5f236d9bea38b8c9048f379fec179b09984c (diff)
downloadrust-bed54cf85457d76f479fcd406f07b39c5e8abb12.tar.gz
rust-bed54cf85457d76f479fcd406f07b39c5e8abb12.zip
rustdoc: set cfg(doctest) when collecting doctests
Diffstat (limited to 'src/doc')
-rw-r--r--src/doc/rustdoc/src/unstable-features.md30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/doc/rustdoc/src/unstable-features.md b/src/doc/rustdoc/src/unstable-features.md
index 3938df1a682..1d9510c9aac 100644
--- a/src/doc/rustdoc/src/unstable-features.md
+++ b/src/doc/rustdoc/src/unstable-features.md
@@ -212,6 +212,36 @@ pub struct BigX;
 Then, when looking for it through the `rustdoc` search, if you enter "x" or
 "big", search will show the `BigX` struct first.
 
+### Include items only when collecting doctests
+
+Rustdoc's [documentation tests] can do some things that regular unit tests can't, so it can
+sometimes be useful to extend your doctests with samples that wouldn't otherwise need to be in
+documentation. To this end, Rustdoc allows you to have certain items only appear when it's
+collecting doctests, so you can utilize doctest functionality without forcing the test to appear in
+docs, or to find an arbitrary private item to include it on.
+
+If you add `#![feature(cfg_doctest)]` to your crate, Rustdoc will set `cfg(doctest)` when collecting
+doctests. Note that they will still link against only the public items of your crate; if you need to
+test private items, unit tests are still the way to go.
+
+In this example, we're adding doctests that we know won't compile, to verify that our struct can
+only take in valid data:
+
+```rust
+#![feature(cfg_doctest)]
+
+/// We have a struct here. Remember it doesn't accept negative numbers!
+pub struct MyStruct(usize);
+
+/// ```compile_fail
+/// let x = my_crate::MyStruct(-5);
+/// ```
+#[cfg(doctest)]
+pub struct MyStructOnlyTakesUsize;
+```
+
+[documentation tests]: documentation-tests.html
+
 ## Unstable command-line arguments
 
 These features are enabled by passing a command-line flag to Rustdoc, but the flags in question are