Skip to content

Commit 4a18fc4

Browse files
authored
Merge pull request torvalds#671 from ojeda/comments
doc: rust: document how to write comments
2 parents 5f41e7b + 34cb8c0 commit 4a18fc4

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Documentation/rust/coding-guidelines.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,69 @@ individual files, and does not require a kernel configuration. Sometimes it may
3939
even work with broken code.
4040

4141

42+
Comments
43+
--------
44+
45+
"Normal" comments (i.e. ``//``, rather than code documentation which starts
46+
with ``///`` or ``//!``) are written in Markdown the same way as documentation
47+
comments are, even though they will not be rendered. This improves consistency,
48+
simplifies the rules and allows to move content between the two kinds of
49+
comments more easily. For instance::
50+
51+
.. code-block:: rust
52+
53+
// `object` is ready to be handled now.
54+
f(object);
55+
56+
Furthermore, just like documentation, comments are capitalized at the beginning
57+
of a sentence and ended with a period (even if it is a single sentence). This
58+
includes `// SAFETY: `, `// TODO: ` and other "tagged" comments, e.g.::
59+
60+
.. code-block:: rust
61+
62+
// FIXME: The error should be handled properly.
63+
64+
Comments should not be used for documentation purposes: comments are intended
65+
for implementation details, not users. This distinction is useful even if the
66+
reader of the source file is both an implementor and a user of an API. In fact,
67+
sometimes it is useful to use both comments and documentation at the same time.
68+
For instance, for a `TODO` list or to comment on the documentation itself. For
69+
the latter case, comments can be inserted in the middle; that is, closer to
70+
the line of documentation to be commented. For any other case, comments are
71+
written after the documentation, e.g.::
72+
73+
.. code-block:: rust
74+
75+
/// Returns a new [`Foo`].
76+
///
77+
/// # Examples
78+
///
79+
// TODO: Find a better example.
80+
/// ```
81+
/// let foo = f(42);
82+
/// ```
83+
// FIXME: Use fallible approach.
84+
pub fn f(x: i32) -> Foo {
85+
// ...
86+
}
87+
88+
One special kind of comments are the `// SAFETY: ` comments. These must appear
89+
before every `unsafe` block, and they explain why the code inside the block is
90+
correct/sound, i.e. why it cannot trigger undefined behavior in any case, e.g.::
91+
92+
.. code-block:: rust
93+
94+
// SAFETY: `p` is valid by the safety requirements.
95+
unsafe { *p = 0; }
96+
97+
`// SAFETY: ` comments are not to be confused with the `# Safety` sections in
98+
code documentation. ``# Safety`` sections specify the contract that callers
99+
(for functions) or implementors (for traits) need to abide by. ``// SAFETY: ``
100+
comments show why a call (for functions) or implementation (for traits) actually
101+
respects the preconditions stated in a ``# Safety`` section or the language
102+
reference.
103+
104+
42105
Code documentation
43106
------------------
44107

0 commit comments

Comments
 (0)