3
3
Docs
4
4
====
5
5
6
- Rust kernel code is not documented like C kernel code (i.e. via kernel-doc).
7
- Instead, we use the usual system for documenting Rust code: the ``rustdoc ``
8
- tool, which uses Markdown (a *very * lightweight markup language).
9
-
10
6
This document describes how to make the most out of the kernel documentation
11
7
for Rust.
12
8
9
+ Rust kernel code is not documented like C kernel code (i.e. via kernel-doc).
10
+ Instead, the usual system for documenting Rust code is used: the ``rustdoc ``
11
+ tool, which uses Markdown (a lightweight markup language).
12
+
13
+ To learn Markdown, there are many guides available out there. For instance,
14
+ the one at:
15
+
16
+ https://commonmark.org/help/
17
+
13
18
14
19
Reading the docs
15
20
----------------
16
21
17
- An advantage of using Markdown is that it attempts to make text look almost as
18
- you would have written it in plain text. This makes the documentation quite
19
- pleasant to read even in its source form.
20
-
21
- However, the generated HTML docs produced by ``rustdoc `` provide a *very * nice
22
- experience, including integrated instant search, clickable items (types,
23
- functions, constants, etc. -- including to all the standard Rust library ones
24
- that we use in the kernel, e.g. ``core ``), categorization, links to the source
25
- code, etc.
22
+ The generated HTML docs produced by ``rustdoc `` include integrated search,
23
+ linked items (e.g. types, functions, constants), source code, etc.
26
24
27
- Like for the rest of the kernel documentation, pregenerated HTML docs for
28
- the libraries (crates) inside ``rust/ `` that are used by the rest of the kernel
29
- are available at `kernel.org `_ (TODO: link when in mainline and generated
30
- alongside the rest of the documentation).
25
+ The generated docs may be read at (TODO: link when in mainline and generated
26
+ alongside the rest of the documentation):
31
27
32
- .. _ kernel.org : http://kernel.org/
28
+ http://kernel.org/
33
29
34
- Otherwise, you can generate them locally. This is quite fast (same order as
35
- compiling the code itself) and you do not need any special tools or environment.
36
- This has the added advantage that they will be tailored to your particular
37
- kernel configuration. To generate them, simply use the ``rustdoc `` target with
38
- the same invocation you use for compilation, e.g.::
30
+ The docs can also be easily generated and read locally. This is quite fast
31
+ (same order as compiling the code itself) and no special tools or environment
32
+ are needed. This has the added advantage that they will be tailored to
33
+ the particular kernel configuration used . To generate them, use the ``rustdoc ``
34
+ target with the same invocation used for compilation, e.g.::
39
35
40
36
make LLVM=1 rustdoc
41
37
42
38
43
39
Writing the docs
44
40
----------------
45
41
46
- If you already know Markdown, learning how to write Rust documentation will be
47
- a breeze. If not, understanding the basics is a matter of minutes reading other
48
- code. There are also many guides available out there, a particularly nice one
49
- is at `GitHub `_.
50
-
51
- .. _GitHub : https://guides.github.com/features/mastering-markdown/#syntax
52
-
53
- This is how a well-documented Rust function may look like (derived from the Rust
54
- standard library)::
42
+ This is how a well-documented Rust function may look like::
55
43
56
44
/// Returns the contained [`Some`] value, consuming the `self` value,
57
45
/// without checking that the value is not [`None`].
@@ -77,32 +65,35 @@ standard library)::
77
65
}
78
66
}
79
67
80
- This example showcases a few ``rustdoc `` features and some common conventions
81
- (that we also follow in the kernel):
68
+ This example showcases a few ``rustdoc `` features and some conventions followed
69
+ in the kernel:
70
+
71
+ - The first paragraph must be a single sentence briefly describing what
72
+ the documented item does. Further explanations must go in extra paragraphs.
82
73
83
- * The first paragraph must be a single sentence briefly describing what
84
- the documented item does. Further explanations must go in extra paragraphs .
74
+ - Unsafe functions must document their safety preconditions under
75
+ a `` # Safety `` section .
85
76
86
- * `` unsafe `` functions must document the preconditions needed for a call to be
87
- safe under a ``Safety `` section.
77
+ - While not shown here, if a function may panic, the conditions under which
78
+ that happens must be described under a ``# Panics `` section.
88
79
89
- * While not shown here, if a function may panic, the conditions under which
90
- that happens must be described under a ``Panics `` section. Please note that
91
- panicking should be very rare and used only with a good reason. In almost
92
- all cases, you should use a fallible approach, returning a `Result `.
80
+ Please note that panicking should be very rare and used only with a good
81
+ reason. In almost all cases, a fallible approach should be used, typically
82
+ returning a ``Result ``.
93
83
94
- * If providing examples of usage would help readers, they must be written in
95
- a section called ``Examples ``.
84
+ - If providing examples of usage would help readers, they must be written in
85
+ a section called ``# Examples ``.
96
86
97
- * Rust items (functions, types, constants...) will be automatically linked
98
- (``rustdoc `` will find out the URL for you ).
87
+ - Rust items (functions, types, constants...) must be linked appropriately
88
+ (``rustdoc `` will create a link automatically ).
99
89
100
- * Following the Rust standard library conventions, any ``unsafe `` block must be
101
- preceded by a `` SAFETY `` comment describing why the code inside is sound.
90
+ - Any ``unsafe `` block must be preceded by a `` // SAFETY: `` comment
91
+ describing why the code inside is sound.
102
92
103
- While sometimes the reason might look trivial and therefore unneeded, writing
104
- these comments is not just a good way of documenting what has been taken into
105
- account, but also that there are no *extra * implicit constraints.
93
+ While sometimes the reason might look trivial and therefore unneeded, writing
94
+ these comments is not just a good way of documenting what has been taken into
95
+ account, but most importantly, it provides a way to know that there are
96
+ no *extra * implicit constraints.
106
97
107
98
To learn more about how to write documentation for Rust and extra features,
108
99
please take a look at the ``rustdoc `` `book `_.
0 commit comments