|
1 | | -.. _rust_docs: |
| 1 | +.. _rust_coding_guidelines: |
2 | 2 |
|
3 | | -Docs |
4 | | -==== |
| 3 | +Coding Guidelines |
| 4 | +================= |
5 | 5 |
|
6 | | -This document describes how to make the most out of the kernel documentation |
7 | | -for Rust. |
| 6 | +This document describes how to write Rust code in the kernel. |
8 | 7 |
|
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 | 8 |
|
13 | | -To learn Markdown, there are many guides available out there. For instance, |
14 | | -the one at: |
| 9 | +Style & formatting |
| 10 | +------------------ |
15 | 11 |
|
16 | | - https://commonmark.org/help/ |
| 12 | +The code should be formatted using ``rustfmt``. In this way, a person |
| 13 | +contributing from time to time to the kernel does not need to learn and |
| 14 | +remember one more style guide. More importantly, reviewers and maintainers |
| 15 | +do not need to spend time pointing out style issues anymore, and thus |
| 16 | +less patch roundtrips may be needed to land a change. |
| 17 | + |
| 18 | +.. note:: Conventions on comments and documentation are not checked by |
| 19 | + ``rustfmt``. Thus those are still needed to be taken care of. |
17 | 20 |
|
| 21 | +The default settings of ``rustfmt`` are used. This means the idiomatic Rust |
| 22 | +style is followed. For instance, 4 spaces are used for indentation rather |
| 23 | +than tabs. |
18 | 24 |
|
19 | | -Reading the docs |
20 | | ----------------- |
| 25 | +It is convenient to instruct editors/IDEs to format while typing, |
| 26 | +when saving or at commit time. However, if for some reason reformatting |
| 27 | +the entire kernel Rust sources is needed at some point, the following can be |
| 28 | +run:: |
21 | 29 |
|
22 | | -The generated HTML docs produced by ``rustdoc`` include integrated search, |
23 | | -linked items (e.g. types, functions, constants), source code, etc. |
| 30 | + make LLVM=1 rustfmt |
24 | 31 |
|
25 | | -The generated docs may be read at (TODO: link when in mainline and generated |
26 | | -alongside the rest of the documentation): |
| 32 | +It is also possible to check if everything is formatted (printing a diff |
| 33 | +otherwise), for instance for a CI, with:: |
27 | 34 |
|
28 | | - http://kernel.org/ |
| 35 | + make LLVM=1 rustfmtcheck |
29 | 36 |
|
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.:: |
| 37 | +Like ``clang-format`` for the rest of the kernel, ``rustfmt`` works on |
| 38 | +individual files, and does not require a kernel configuration. Sometimes it may |
| 39 | +even work with broken code. |
35 | 40 |
|
36 | | - make LLVM=1 rustdoc |
37 | 41 |
|
38 | | -To read the docs locally in your web browser, run e.g.:: |
| 42 | +Code documentation |
| 43 | +------------------ |
39 | 44 |
|
40 | | - xdg-open rust/doc/kernel/index.html |
| 45 | +Rust kernel code is not documented like C kernel code (i.e. via kernel-doc). |
| 46 | +Instead, the usual system for documenting Rust code is used: the ``rustdoc`` |
| 47 | +tool, which uses Markdown (a lightweight markup language). |
41 | 48 |
|
42 | | -Writing the docs |
43 | | ----------------- |
| 49 | +To learn Markdown, there are many guides available out there. For instance, |
| 50 | +the one at: |
| 51 | + |
| 52 | + https://commonmark.org/help/ |
44 | 53 |
|
45 | 54 | This is how a well-documented Rust function may look like:: |
46 | 55 |
|
|
0 commit comments