Skip to content

Commit d91d005

Browse files
committed
Fix typos, move example link above the example
1 parent 8fb36bd commit d91d005

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/documentation/safety-comments.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Safety comments
22

3-
Using [`unsafe`] blocks in often required in the Rust compiler or standard
3+
Using [`unsafe`] blocks is often required in the Rust compiler or standard
44
library, but this is not done without rules: each `unsafe` block should have
55
a `SAFETY:` comment explaining why the block is safe, which invariants are
66
used and must be respected. Below are some examples taken from the standard
@@ -15,6 +15,8 @@ caller with the use of documentation in a `# Safety` section while still having
1515
more invariants needed that are not required from callers. `clippy` has a
1616
lint for `# Safety` sections by the way.
1717

18+
[See the example on github][as_bytes_mut]
19+
1820
```rust
1921
/// Converts a mutable string slice to a mutable byte slice.
2022
///
@@ -35,8 +37,6 @@ pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
3537
}
3638
```
3739

38-
[See the function on github][as_bytes_mut]
39-
4040
This example is for a function but the same principle applies to `unsafe trait`s
4141
like [`Send`] or [`Sync`] for example, though they have no `# Safety` section
4242
since their entire documentation is about why they are `unsafe`.
@@ -54,14 +54,16 @@ their `unsafe` parts.
5454
## Inside *safe* elements
5555

5656
Inside safe elements, a `SAFETY:` comment must not depend on anything from the
57-
caller beside properly contruscted types and values (i.e, if your function
58-
receive a reference that is unaligned or null, it's the caller fault if it fails
59-
and not yours).
57+
caller beside properly constructed types and values (i.e, if your function
58+
receives a reference that is unaligned or null, it is the caller fault if it
59+
fails and not yours).
6060

6161
`SAFETY` comments in *safe* elements often rely on checks that are done before
6262
the `unsafe` block or on type invariants, like a division by `NonZeroU8` would
6363
not check for `0` before dividing.
6464

65+
[See the example on github][split_at]
66+
6567
```rust
6668
pub fn split_at(&self, mid: usize) -> (&str, &str) {
6769
// is_char_boundary checks that the index is in [0, .len()]
@@ -74,6 +76,4 @@ pub fn split_at(&self, mid: usize) -> (&str, &str) {
7476
}
7577
```
7678

77-
[See the function on github][split_at]
78-
7979
[split_at]: https://github.com/rust-lang/rust/blob/a08f25a7ef2800af5525762e981c24d96c14febe/library/core/src/str/mod.rs#L570

0 commit comments

Comments
 (0)