Skip to content

Commit d333df4

Browse files
committed
Merge pull request #21041 from steveklabnik/gh17554
Add explanation of main to rustdoc docs Reviewed-by: alexcrichton
2 parents 19ffbbc + d7999d4 commit d333df4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/doc/rustdoc.md

+29
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,35 @@ spawn(move || { fib(200); })
217217
The documentation online would look like `spawn(move || { fib(200); })`, but when
218218
testing this code, the `fib` function will be included (so it can compile).
219219

220+
Rustdoc will automatically add a `main()` wrapper around your code, and in the right
221+
place. For example:
222+
223+
```
224+
/// ```
225+
/// use std::rc::Rc;
226+
///
227+
/// let five = Rc::new(5);
228+
/// ```
229+
```
230+
231+
This will end up testing:
232+
233+
```
234+
235+
fn main() {
236+
use std::rc:Rc;
237+
let five = Rc::new(5);
238+
}
239+
```
240+
241+
Here's the full algorithm:
242+
243+
1. Given a code block, if it does not contain `fn main`, it is wrapped in `fn main() { your_code }`
244+
2. Given that result, if it contains no `extern crate` directives but it also
245+
contains the name of the crate being tested, then `extern crate <name>` is
246+
injected at the top.
247+
3. Some common `allow` attributes are added for documentation examples at the top.
248+
220249
## Running tests (advanced)
221250

222251
Running tests often requires some special configuration to filter tests, find

0 commit comments

Comments
 (0)