|
| 1 | +# How to write documentation |
| 2 | + |
| 3 | +This document explains how to write documentation for the std/core public APIs. |
| 4 | + |
| 5 | +Let's start with some general information: |
| 6 | + |
| 7 | +### Contractions |
| 8 | + |
| 9 | +It is common in english to have contractions such "don't" or "can't". They can |
| 10 | +never be used in documentation. Always write their "full form": |
| 11 | + |
| 12 | + * "do not" instead of "don't" |
| 13 | + * "cannot" instead of "can't" |
| 14 | + * "it would" instead of "it'd" |
| 15 | + * "it will" instead of "it'll" |
| 16 | + * "you are" instead of "you're" |
| 17 | + * "he is"/"he has" instead of "he's" |
| 18 | + * etc |
| 19 | + |
| 20 | +The only exception to this rule is "let's" as it is specific/known/common enough. |
| 21 | + |
| 22 | +The reason is simply to make the reading simpler as many readers are not english |
| 23 | +native or could have reading issues. |
| 24 | + |
| 25 | +### When to use inline code blocks |
| 26 | + |
| 27 | +Whenever you are talking about a type or anything code related, it should be in a |
| 28 | +inline code block. As a reminder, a inline code block is created with backticks |
| 29 | +(\`). For example: |
| 30 | + |
| 31 | + |
| 32 | +```text |
| 33 | +This a `Vec` and it has a method `push` which you can call by doing `Vec::push`. |
| 34 | +``` |
| 35 | + |
| 36 | +### When to use intra-doc links |
| 37 | + |
| 38 | +Intra-doc links (you can see the full explanations for the feature |
| 39 | +[here](https://doc.rust-lang.org/rustdoc/write-documentation/linking-to-items-by-name.html)) |
| 40 | +should be used as much as possible whenever a type is mentioned. |
| 41 | + |
| 42 | +Little note: when you are documenting an item, no need to link to it. So if you |
| 43 | +write documentation for `String::push_str` method, no need to link to the method |
| 44 | +`push_str` or to the `String` type. |
| 45 | + |
| 46 | +If you have cases like `Vec<String>`, you need to use intra-doc links on both |
| 47 | +`Vec` and `String` as well. It would look like this: |
| 48 | + |
| 49 | +```text |
| 50 | +This is a [`Vec`]`<`[`String`]`>`. |
| 51 | +``` |
| 52 | + |
| 53 | +Extra explanations: since both `Vec` and `String` are in codeblocks, `<` and `>` |
| 54 | +should as well, otherwise it would render badly. |
| 55 | + |
| 56 | +### Code blocks |
| 57 | + |
| 58 | +With rustdoc, code blocks are tested (because considered as Rust code blocks by |
| 59 | +default). It allows us to know if the documentation is up to date. As such, |
| 60 | +please avoid to use `ignore` as much as possible on code blocks! If you want to |
| 61 | +write into another language than Rust, simply set it in the code block tags: |
| 62 | + |
| 63 | + ```text |
| 64 | + This is not rust code! |
| 65 | + ``` |
| 66 | + |
| 67 | +Some special cases: |
| 68 | + * If the code example cannot be run (when documenting a I/O item for example), |
| 69 | + use `no_run`. |
| 70 | + * If it is expected to fail, use `should_panic`. |
| 71 | + * If it is expected to fail compilation (which be quite rare!), use `compile_fail`. |
| 72 | + |
| 73 | +You can find more information about code blocks |
| 74 | +[here](https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html). |
| 75 | + |
| 76 | +## How to write documentation for a module |
| 77 | + |
| 78 | +A module is supposed to contain "similar" items. As such, its documentation is |
| 79 | +supposed to give an overview and eventually **a base** to understand what the |
| 80 | +items it contains are doing. |
| 81 | + |
| 82 | +You can take a look at the |
| 83 | +[f32 module](https://doc.rust-lang.org/nightly/std/f32/index.html) or at the |
| 84 | +[fmt module](https://doc.rust-lang.org/nightly/std/fmt/index.html) to see |
| 85 | +both cases. |
| 86 | + |
| 87 | +## How to write documentation for functions/methods |
| 88 | + |
| 89 | +The basic format of each documented methods/functions should roughly look like this: |
| 90 | + |
| 91 | +```text |
| 92 | +[explanations] |
| 93 | +
|
| 94 | +[example(s)] |
| 95 | +``` |
| 96 | + |
| 97 | +### Explanations |
| 98 | + |
| 99 | +By `explanations` we mean that the text should explain what the method and what |
| 100 | +each of its arguments are for. Let's take this method for example: |
| 101 | + |
| 102 | +```rust |
| 103 | +pub fn concat_str(&self, s: &str) -> String { |
| 104 | + if s.is_empty() { |
| 105 | + panic!("empty concat string"); |
| 106 | + } |
| 107 | + format!("{}{}", self.string, s) |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +The explanation should look like this: |
| 112 | + |
| 113 | +```text |
| 114 | +Returns a new [`String`] which contains `&self` content with `s` added at the end. |
| 115 | +``` |
| 116 | + |
| 117 | +### Panic? |
| 118 | + |
| 119 | +If the function/method can panic in certain circumstances, it must to be |
| 120 | +mentioned! This explanation needs to be prepended by a `Panics` title: |
| 121 | + |
| 122 | +```text |
| 123 | +# Panics |
| 124 | +
|
| 125 | +`concat_str` panics if `s` is empty. |
| 126 | +``` |
| 127 | + |
| 128 | +### Examples |
| 129 | + |
| 130 | +As for the examples, they have to show the usage of the function/method. Just |
| 131 | +like the `panic` section, they need to be prepended by a `Examples` title. |
| 132 | + |
| 133 | +It is better if you use `assert*!` macros at the end to ensure that the example |
| 134 | +is working as expected. It also allows the readers to understand more easily |
| 135 | +what the function is doing (or returning). |
| 136 | + |
| 137 | + # Examples |
| 138 | + |
| 139 | + ``` |
| 140 | + let s = MyType::new("hello "); |
| 141 | + assert_eq!("hello Georges", s.concat_str("Georges").as_str()); |
| 142 | + ``` |
| 143 | + |
| 144 | +## How to write documentation for other items |
| 145 | + |
| 146 | +It is mostly the same as for methods and functions except that the examples |
| 147 | +are (strongly) recommended and not mandatory. |
| 148 | + |
| 149 | +A good example often shows how to create the item. |
0 commit comments