Skip to content

Add explanation of main to rustdoc docs #21041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/doc/rustdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,35 @@ spawn(move || { fib(200); })
The documentation online would look like `spawn(move || { fib(200); })`, but when
testing this code, the `fib` function will be included (so it can compile).

Rustdoc will automatically add a `main()` wrapper around your code, and in the right
place. For example:

```
/// ```
/// use std::rc::Rc;
///
/// let five = Rc::new(5);
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing item after doc. @huonw suggests # fn foo() {}.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh that's weird. This actually does work when processing doc comments though...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i'm an idiot

# fn foo() {}
```

This will end up testing:

```
fn main() {
use std::rc::Rc;
let five = Rc::new(5);
}
```

Here's the full algorithm:

1. Given a code block, if it does not contain `fn main`, it is wrapped in `fn main() { your_code }`
2. Given that result, if it contains no `extern crate` directives but it also
contains the name of the crate being tested, then `extern crate <name>` is
injected at the top.
3. Some common `allow` attributes are added for documentation examples at the top.

## Running tests (advanced)

Running tests often requires some special configuration to filter tests, find
Expand Down