Skip to content

Commit ca07c2e

Browse files
committed
trpl: explain how to inhibit rustdoc's auto-main
I think this fixes #30137. I basically just repeated some details that were scattered around other places in this document, and emphasized that you probably don't want an `extern crate` or `mod` statement to end up inside a function.
1 parent eb1d018 commit ca07c2e

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/doc/book/documentation.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ Let's discuss our sample example documentation:
213213
```
214214

215215
You'll notice that you don't need a `fn main()` or anything here. `rustdoc` will
216-
automatically add a `main()` wrapper around your code, and in the right place.
217-
For example:
216+
automatically add a `main()` wrapper around your code, using heuristics to attempt
217+
to put it in the right place. For example:
218218

219219
```rust
220220
/// ```
@@ -242,11 +242,18 @@ Here's the full algorithm rustdoc uses to preprocess examples:
242242
`unused_attributes`, and `dead_code`. Small examples often trigger
243243
these lints.
244244
3. If the example does not contain `extern crate`, then `extern crate
245-
<mycrate>;` is inserted.
246-
2. Finally, if the example does not contain `fn main`, the remainder of the
247-
text is wrapped in `fn main() { your_code }`
248-
249-
Sometimes, this isn't enough, though. For example, all of these code samples
245+
<mycrate>;` is inserted (note the lack of `#[macro_use]`).
246+
4. Finally, if the example does not contain `fn main`, the remainder of the
247+
text is wrapped in `fn main() { your_code }`.
248+
249+
This generated `fn main` can be a problem! If you have `extern crate` or a `mod`
250+
statements in the example code that are referred to by `use` statements, they will
251+
fail to resolve unless you include at least `fn main() {}` to inhibit step 4.
252+
`#[macro_use] extern crate` also does not work except at the crate root, so when
253+
testing macros an explicit `main` is always required. It doesn't have to clutter
254+
up your docs, though -- keep reading!
255+
256+
Sometimes this algorithm isn't enough, though. For example, all of these code samples
250257
with `///` we've been talking about? The raw text:
251258

252259
```text
@@ -370,8 +377,8 @@ macro_rules! panic_unless {
370377

371378
You’ll note three things: we need to add our own `extern crate` line, so that
372379
we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
373-
`main()` as well. Finally, a judicious use of `#` to comment out those two
374-
things, so they don’t show up in the output.
380+
`main()` as well (for reasons discussed above). Finally, a judicious use of
381+
`#` to comment out those two things, so they don’t show up in the output.
375382

376383
Another case where the use of `#` is handy is when you want to ignore
377384
error handling. Lets say you want the following,

0 commit comments

Comments
 (0)