Change:
Here's the full algorithm: <del> 1. Given a code block, if it does not contain `fn main`, it is wrapped in `fn main() { your_code }` </del> <ins> 1. Given a code block, if it does not contain `fn main()`, it is wrapped in `fn main() { your_code }` </ins> 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. <del> 3. Some common `allow` attributes are added for documentation examples at the top. </del> <ins> 3. Some common allow attributes are added for documentation examples at the top. Sometimes, this isn't enough, though. For example, all of these code samples with `///` we've been talking about? The raw text: ```text /// Some documentation. # fn foo() {} ``` looks different than the output: ``` /// Some documentation. # fn foo() {} ``` Yes, that's right: you can add lines that start with `# `, and they will be hidden from the output, but will be used when compiling your code. You can use this to your advantage. In this case, documentation comments need to apply to some kind of function, so if I want to show you just a documentation comment, I need to add a little function definition below it. At the same time, it's just there to satisfy the compiler, so hiding it makes the example more clear. You can use this technique to explain longer examples in detail, while still preserving the testability of your documentation. For example, this code: ``` let x = 5; let y = 6; println!("{}", x + y); ``` Here's an explanation, rendered: First, we set `x` to five: ``` let x = 5; # let y = 6; # println!("{}", x + y); ``` Next, we set `y` to six: ``` # let x = 5; let y = 6; # println!("{}", x + y); ``` Finally, we print the sum of `x` and `y`: ``` # let x = 5; # let y = 6; println!("{}", x + y); ``` Here's the same explanation, in raw text: > First, we set `x` to five: > > ```text > let x = 5; > # let y = 6; > # println!("{}", x + y); > ``` > > Next, we set `y` to six: > > ```text > # let x = 5; > let y = 6; > # println!("{}", x + y); > ``` > > Finally, we print the sum of `x` and `y`: > > ```text > # let x = 5; > # let y = 6; > println!("{}", x + y); > ``` By repeating all parts of the example, you can ensure that your example still compiles, while only showing the parts that are relevant to that part of your explanation. To run the tests, either ```bash $ rustdoc --test path/to/my/crate/root.rs # or $ cargo test ``` That's right, `cargo test` tests embedded documentation too. There are a few more annotations that are useful to help `rustdoc` do the right thing when testing your code: ``` /// ```ignore /// fn foo() { /// ``` # fn foo() {} ``` The `ignore` directive tells Rust to ignore your code. This is almost never what you want, as it's the most generic. Instead, consider annotating it with `text` if it's not code, or using `#`s to get a working example that only shows the part you care about. ``` /// ```should_panic /// assert!(false); /// ``` # fn foo() {} ``` `should_panic` tells `rustdoc` that the code should compile correctly, but not actually pass as a test. ``` /// ```no_run /// loop { ///     println!("Hello, world"); /// } /// ``` # fn foo() {} ``` The `no_run` attribute will compile your code, but not run it. This is important for examples such as "Here's how to start up a network service," which you would want to make sure compile, but might run in an infinite loop! ### Documenting modules Rust has another kind of doc comment, `//!`. This comment doesn't document the next item, but the enclosing item. In other words: ``` mod foo { //! This is documentation for the `foo` module. //! //! # Examples // ... } ``` This is where you'll see `//!` used most often: for module documentation. If you have a module in `foo.rs`, you'll often open its code and see this: ``` //! A module for using `foo`s. //! //! The `foo` module contains a lot of useful functionality blah blah blah ``` ### Documentation comment style Check out [RFC 505][rfc505] for full conventions around the style and format of documentation. [rfc505]: https://github.com/rust-lang/rfcs/blob/master/text/0505-api-comment-conventions.md ## Other documentation All of this behavior works in non-Rust source files too. Because comments are written in Markdown, they're often `.md` files. When you write documentation in Markdown files, you don't need to prefix the documentation with comments. For example: </ins> <del> ## Running tests (advanced) </del> <ins> ``` /// # Examples /// /// ``` /// use std::rc::Rc; /// /// let five = Rc::new(5); /// ``` # fn foo() {} ``` </ins> <del> Running tests often requires some special configuration to filter tests, find libraries, or try running ignored examples. The testing framework that rustdoc uses is built on crate `test`, which is also used when you compile crates with rustc's `--test` flag. Extra arguments can be passed to rustdoc's test harness with the `--test-args` flag. </del> <ins> is just </ins> <del> ~~~console # Only run tests containing 'foo' in their name $ rustdoc --test lib.rs --test-args 'foo' </del> <ins> ~~~markdown # Examples </ins> <del> # See what's possible when running tests $ rustdoc --test lib.rs --test-args '--help' </del> <ins> ``` use std::rc::Rc; let five = Rc::new(5); ``` </ins> ~~~ <del> When testing a library, code examples will often show how functions are used, and this code often requires `use`-ing paths from the crate. To accommodate this, rustdoc will implicitly add `extern crate <crate>;` where `<crate>` is the name of the crate being tested to the top of each code example. This means that rustdoc must be able to find a compiled version of the library crate being tested. Extra search paths may be added via the `-L` flag to `rustdoc`. </del> <ins> when it's in a Markdown file. There is one wrinkle though: Markdown files need to have a title like this: </ins> <del> # Standalone Markdown files </del> <ins> ```markdown % The title </ins> <del> As well as Rust crates, rustdoc supports rendering pure Markdown files into HTML and testing the code snippets from them. A Markdown file is detected by a `.md` or `.markdown` extension. </del> <ins> This is the example documentation. ``` This `%` line needs to be the very first line of the file. ## `doc` attributes At a deeper level, documentation comments are sugar for documentation attributes: ``` /// this # fn foo() {} #[doc="this"] # fn bar() {} ``` </ins> <del> There are 4 options to modify the output that Rustdoc creates. </del> <ins> are the same, as are these: </ins> <del> - `--markdown-css PATH`: adds a `<link rel="stylesheet">` tag pointing to `PATH`. - `--html-in-header FILE`: includes the contents of `FILE` at the end of the `<head>...</head>` section. - `--html-before-content FILE`: includes the contents of `FILE` directly after `<body>`, before the rendered content (including the title). - `--html-after-content FILE`: includes the contents of `FILE` directly before `</body>`, after all the rendered content. </del> <ins> ``` //! this </ins> <del> All of these can be specified multiple times, and they are output in the order in which they are specified. The first line of the file being rendered must be the title, prefixed with `%` (e.g. this page has `% Rust Documentation` on the first line). </del> <ins> #![doc="/// this"] ``` </ins> <del> Like with a Rust crate, the `--test` argument will run the code examples to check they compile, and obeys any `--test-args` flags. The tests are named after the last `#` heading. </del> <ins> You won't often see this attribute used for writing documentation, but it can be useful when changing some options, or when writing a macro. </ins> <del> # Re-exports </del> <ins> ### Re-exports </ins> <del> Rustdoc will show the documentation for a publc re-export in both places: </del> <ins> `rustdoc` will show the documentation for a publc re-export in both places: </ins> <del> ```{rust,ignore} </del> <ins> ```ignore </ins> extern crate foo; pub use foo::bar; ``` <del> This will create documentation for `bar` both inside the documentation for the crate `foo`, as well as the documentation for your crate. It will use the same documentation in both places. </del> <ins> This will create documentation for bar both inside the documentation for the crate `foo`, as well as the documentation for your crate. It will use the same documentation in both places. </ins> This behavior can be supressed with `no_inline`: <del> ```{rust,ignore} </del> <ins> ```ignore </ins> extern crate foo; #[doc(no_inline)] pub use foo::bar; ``` <ins> ### Controlling HTML You can control a few aspects of the HTML that `rustdoc` generates through the `#![doc]` version of the attribute: ``` #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/")]; ``` This sets a few different options, with a logo, favicon, and a root URL. ## Generation options `rustdoc` also contains a few other options on the command line, for further customiziation: - `--html-in-header FILE`: includes the contents of FILE at the end of the `<head>...</head>` section. - `--html-before-content FILE`: includes the contents of FILE directly after `<body>`, before the rendered content (including the search bar). - `--html-after-content FILE`: includes the contents of FILE after all the rendered content. </ins>

Discussion:
E.g. , which is very extensive, and even includes a redefinition of the type! On the other hand is more detailed (i'll note that these aren't precisely equivalent, since is in that module too). Similarly, .
cc
Other somewhat relevant modules: (both module and type are rather empty atm, however) the various primitive types the other containers (most are pretty much undocumented too, unfortunately)
My gut says "Full docs in the module, types should mention 'see module for full docs'
cc me
Steve's gut sounds right to me. Would especially work better if we had sane intra-links.
Although doc'ing the type can be more "portable" in the face of re-exports.
Hm, so I'm now starting to prefer the other direction. Module-level docs are great for telling an overall narrative for people who are exploring, but they are not good when people are looking up a type/method etc. I guess the ideal option would be "both"? But I'm not really sure. As says links could help...

We should have guidelines for how docs are written. Tense, vs , all that stuff.
Fully agree. Among the things it should cover: What a docstring is First paragraph is summary Summary should usually be a single sentence, and should always have a trailing period At least for functions. Our modules almost never have the trailing period. That seems ok to me, but it's all subjective Tense, e.g. "Create a Foo" vs "Creates a Foo" Tense in summary different than tense in rest of documentation? It sometimes is. Is that ok? Proper use of inline code spans whenever referencing e.g. types, variables Proper use of code blocks (<code</code, not <code~~~~</code) Code block modifiers, e.g. , , Common headers seen in docstrings, e.g. ,
cc: Particularly .
I would like to aggregate all these ideas into a single draft document. What are your thoughts about it being an additional section to ? I believe that document needs a lot of work, and adding a rustdoc style guide would help greatly.
They should certainly be related.
It would be nice if we could settle on these things. I want to contribute to the documentation and I end up mimicking the current style in the source files, but that style differs. There's also the issue of vs , see .
I don't know if this is something we care about, but if there is a preference towards British or American spelling, I think it should be a stated part of this guide. is what made me think of this (<3 ;)
Yes, it is something we should care about. I often spell things with British colour though :sweat_smile:
Good point. My belief is that the documentation to date has been using american spelling, because I feel like I would remember if I saw British spelling in it. It would be good to codify this. Personally, I don't care which spelling rules are chosen as long as it's consistent, but given that I believe the docs are using american spelling right now, I would suggest sticking with that.