33Docs
44====
55
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-
106This document describes how to make the most out of the kernel documentation
117for Rust.
128
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+
1318
1419Reading the docs
1520----------------
1621
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.
2624
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):
3127
32- .. _ kernel.org : http://kernel.org/
28+ http://kernel.org/
3329
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.::
3935
4036 make LLVM=1 rustdoc
4137
4238
4339Writing the docs
4440----------------
4541
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::
5543
5644 /// Returns the contained [`Some`] value, consuming the `self` value,
5745 /// without checking that the value is not [`None`].
@@ -77,32 +65,35 @@ standard library)::
7765 }
7866 }
7967
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.
8273
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 .
8576
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.
8879
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 ``.
9383
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 ``.
9686
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 ).
9989
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.
10292
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.
10697
10798To learn more about how to write documentation for Rust and extra features,
10899please take a look at the ``rustdoc `` `book `_.
0 commit comments