1- .. _rust_coding_guidelines :
2-
31Coding Guidelines
42=================
53
@@ -46,7 +44,7 @@ Comments
4644with ``/// `` or ``//! ``) are written in Markdown the same way as documentation
4745comments are, even though they will not be rendered. This improves consistency,
4846simplifies the rules and allows to move content between the two kinds of
49- comments more easily. For instance::
47+ comments more easily. For instance:
5048
5149.. code-block :: rust
5250
@@ -55,7 +53,7 @@ comments more easily. For instance::
5553
5654 Furthermore, just like documentation, comments are capitalized at the beginning
5755of 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.:
5957
6058.. code-block :: rust
6159
@@ -65,10 +63,10 @@ Comments should not be used for documentation purposes: comments are intended
6563for implementation details, not users. This distinction is useful even if the
6664reader of the source file is both an implementor and a user of an API. In fact,
6765sometimes 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
7068the 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.:
7270
7371.. code-block :: rust
7472
@@ -82,21 +80,21 @@ written after the documentation, e.g.::
8280 /// ```
8381 // FIXME: Use fallible approach.
8482 pub fn f(x: i32) -> Foo {
85- // ...
83+ // ...
8684 }
8785
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.:
9189
9290.. code-block :: rust
9391
9492 // SAFETY: `p` is valid by the safety requirements.
9593 unsafe { *p = 0; }
9694
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: ``
10098comments show why a call (for functions) or implementation (for traits) actually
10199respects the preconditions stated in a ``# Safety `` section or the language
102100reference.
@@ -134,12 +132,12 @@ This is how a well-documented Rust function may look like:
134132 /// assert_eq!(unsafe { x.unwrap_unchecked() }, "air");
135133 /// ```
136134 pub unsafe fn unwrap_unchecked(self) -> T {
137- match self {
138- Some(val) => val,
135+ match self {
136+ Some(val) => val,
139137
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+ }
143141 }
144142
145143 This example showcases a few ``rustdoc `` features and some conventions followed
@@ -167,15 +165,15 @@ in the kernel:
167165 - Any ``unsafe `` block must be preceded by a ``// SAFETY: `` comment
168166 describing why the code inside is sound.
169167
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.
174172
175173To 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:
177175
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
179177
180178
181179Naming
0 commit comments