3 Use `cargo doc` to build documentation in `target/doc`, `cargo doc --open`
4 will automatically open it in your web browser.
6 Use `cargo test` to run all tests (including documentation tests), and `cargo
7 test --doc` to only run documentation tests.
9 These commands will appropriately invoke `rustdoc` (and `rustc`) as required.
13 Doc comments are very useful for big projects that require documentation. When
14 running `rustdoc`, these are the comments that get compiled into
15 documentation. They are denoted by a `///`, and support [Markdown].
17 ````rust,editable,ignore
18 #![crate_name = "doc"]
20 /// A human being is represented here
22 /// A person must have a name, no matter how much Juliet may hate it
27 /// Creates a person with the given name.
32 /// // You can have rust code between fences inside the comments
33 /// // If you pass --test to `rustdoc`, it will even test it for you!
35 /// let person = Person::new("name");
37 pub fn new(name: &str) -> Person {
39 name: name.to_string(),
43 /// Gives a friendly hello!
45 /// Says "Hello, [name](Person::name)" to the `Person` it is called on.
47 println!("Hello, {}!", self.name);
52 let john = Person::new("John");
58 To run the tests, first build the code as a library, then tell `rustdoc` where
59 to find the library so it can link it into each doctest program:
62 $ rustc doc.rs --crate-type lib
63 $ rustdoc --test --extern doc="libdoc.rlib" doc.rs
68 Below are a few examples of the most common `#[doc]` attributes used with
73 Used to inline docs, instead of linking out to separate page.
88 Used to prevent linking out to separate page or anywhere.
91 // Example from libcore/prelude
93 pub use crate::mem::drop;
98 Using this tells `rustdoc` not to include this in documentation:
100 ```rust,editable,ignore
101 // Example from the futures-rs library
103 pub use self::async_await::*;
106 For documentation, `rustdoc` is widely used by the community. It's what is used
107 to generate the [std library docs](https://doc.rust-lang.org/std/).
111 - [The Rust Book: Making Useful Documentation Comments][book]
112 - [The rustdoc Book][rustdoc-book]
113 - [The Reference: Doc comments][ref-comments]
114 - [RFC 1574: API Documentation Conventions][api-conv]
115 - [RFC 1946: Relative links to other items from doc comments (intra-rustdoc links)][intra-links]
116 - [Is there any documentation style guide for comments? (reddit)][reddit]
118 [markdown]: https://en.wikipedia.org/wiki/Markdown
119 [book]: https://doc.rust-lang.org/book/ch14-02-publishing-to-crates-io.html#making-useful-documentation-comments
120 [ref-comments]: https://doc.rust-lang.org/stable/reference/comments.html#doc-comments
121 [rustdoc-book]: https://doc.rust-lang.org/rustdoc/index.html
122 [api-conv]: https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text
123 [intra-links]: https://rust-lang.github.io/rfcs/1946-intra-rustdoc-links.html
124 [reddit]: https://www.reddit.com/r/rust/comments/ahb50s/is_there_any_documentation_style_guide_for/