1
- .. _rust_coding_guidelines :
2
-
3
1
Coding Guidelines
4
2
=================
5
3
@@ -46,7 +44,7 @@ Comments
46
44
with ``/// `` or ``//! ``) are written in Markdown the same way as documentation
47
45
comments are, even though they will not be rendered. This improves consistency,
48
46
simplifies the rules and allows to move content between the two kinds of
49
- comments more easily. For instance::
47
+ comments more easily. For instance:
50
48
51
49
.. code-block :: rust
52
50
@@ -55,7 +53,7 @@ comments more easily. For instance::
55
53
56
54
Furthermore, just like documentation, comments are capitalized at the beginning
57
55
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.: :
56
+ includes `` // SAFETY: `` , `` // TODO: `` and other "tagged" comments, e.g.:
59
57
60
58
.. code-block :: rust
61
59
@@ -65,10 +63,10 @@ Comments should not be used for documentation purposes: comments are intended
65
63
for implementation details, not users. This distinction is useful even if the
66
64
reader of the source file is both an implementor and a user of an API. In fact,
67
65
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
66
+ For instance, for a `` TODO `` list or to comment on the documentation itself.
67
+ For the latter case, comments can be inserted in the middle; that is, closer to
70
68
the line of documentation to be commented. For any other case, comments are
71
- written after the documentation, e.g.::
69
+ written after the documentation, e.g.:
72
70
73
71
.. code-block :: rust
74
72
@@ -82,21 +80,21 @@ written after the documentation, e.g.::
82
80
/// ```
83
81
// FIXME: Use fallible approach.
84
82
pub fn f(x: i32) -> Foo {
85
- // ...
83
+ // ...
86
84
}
87
85
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.::
86
+ One special kind of comments are the `` // SAFETY: ` ` comments. These must appear
87
+ before every `` unsafe ` ` block, and they explain why the code inside the block is
88
+ correct/sound, i.e. why it cannot trigger undefined behavior in any case, e.g.:
91
89
92
90
.. code-block :: rust
93
91
94
92
// SAFETY: `p` is valid by the safety requirements.
95
93
unsafe { *p = 0; }
96
94
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: ``
95
+ `` // SAFETY: `` comments are not to be confused with the `` # Safety `` sections
96
+ in code documentation. ``# Safety `` sections specify the contract that callers
97
+ (for functions) or implementors (for traits) need to abide by. ``// SAFETY: ``
100
98
comments show why a call (for functions) or implementation (for traits) actually
101
99
respects the preconditions stated in a ``# Safety `` section or the language
102
100
reference.
@@ -134,12 +132,12 @@ This is how a well-documented Rust function may look like:
134
132
/// assert_eq!(unsafe { x.unwrap_unchecked() }, "air");
135
133
/// ```
136
134
pub unsafe fn unwrap_unchecked(self) -> T {
137
- match self {
138
- Some(val) => val,
135
+ match self {
136
+ Some(val) => val,
139
137
140
- // SAFETY: The safety contract must be upheld by the caller.
141
- None => unsafe { hint::unreachable_unchecked() },
142
- }
138
+ // SAFETY: The safety contract must be upheld by the caller.
139
+ None => unsafe { hint::unreachable_unchecked() },
140
+ }
143
141
}
144
142
145
143
This example showcases a few ``rustdoc `` features and some conventions followed
@@ -167,15 +165,15 @@ in the kernel:
167
165
- Any ``unsafe `` block must be preceded by a ``// SAFETY: `` comment
168
166
describing why the code inside is sound.
169
167
170
- While sometimes the reason might look trivial and therefore unneeded, writing
171
- these comments is not just a good way of documenting what has been taken into
172
- account, but most importantly, it provides a way to know that there are
173
- no *extra * implicit constraints.
168
+ While sometimes the reason might look trivial and therefore unneeded,
169
+ writing these comments is not just a good way of documenting what has been
170
+ taken into account, but most importantly, it provides a way to know that
171
+ there are no *extra * implicit constraints.
174
172
175
173
To learn more about how to write documentation for Rust and extra features,
176
- please take a look at the ``rustdoc `` ` book `_.
174
+ please take a look at the ``rustdoc `` book at:
177
175
178
- .. _ book : https://doc.rust-lang.org/rustdoc/how-to-write-documentation.html
176
+ https://doc.rust-lang.org/rustdoc/how-to-write-documentation.html
179
177
180
178
181
179
Naming
0 commit comments