Skip to content

Commit

Permalink
Add some further edits to #2433
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Dec 10, 2020
1 parent 14e2174 commit b34bb93
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/ch19-01-unsafe-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,11 @@ that data accessed from different threads is done safely.

### Implementing an Unsafe Trait

Another use-case for `unsafe` is implementing an unsafe trait.
A trait is unsafe when at least one of its methods has some invariant that the
compiler can’t verify. We can declare that a trait is `unsafe` by adding the
`unsafe` keyword before `trait` and marking the implementation of the trait as
`unsafe` too, as shown in Listing 19-11.
Another use case for `unsafe` is implementing an unsafe trait. A trait is
unsafe when at least one of its methods has some invariant that the compiler
can’t verify. We can declare that a trait is `unsafe` by adding the `unsafe`
keyword before `trait` and marking the implementation of the trait as `unsafe`
too, as shown in Listing 19-11.

```rust,unsafe
{{#rustdoc_include ../listings/ch19-advanced-features/listing-19-11/src/main.rs}}
Expand All @@ -437,12 +437,12 @@ those checks manually and indicate as such with `unsafe`.

### Accessing Fields of a Union

The final action that works only with `unsafe` is accessing fields of a `union`
which is similar to a `struct`, but with only one declared field being used
in a particular instance at one time. Unions are primarily used to interface with
unions in C code. Accessing union fields is unsafe because Rust can’t guarantee
the type of the data currently being stored in the union instance. You can
learn more about unions in [the reference][reference].
The final action that works only with `unsafe` is accessing fields of a
*union*. A `union` is similar to a `struct`, but only one declared field is
used in a particular instance at one time. Unions are primarily used to
interface with unions in C code. Accessing union fields is unsafe because Rust
can’t guarantee the type of the data currently being stored in the union
instance. You can learn more about unions in [the reference][reference].

### When to Use Unsafe Code

Expand All @@ -458,6 +458,5 @@ ch04-02-references-and-borrowing.html#dangling-references
ch03-01-variables-and-mutability.html#differences-between-variables-and-constants
[extensible-concurrency-with-the-sync-and-send-traits]:
ch16-04-extensible-concurrency-sync-and-send.html#extensible-concurrency-with-the-sync-and-send-traits

[the-slice-type]: ch04-03-slices.html#the-slice-type
[reference]: ../reference/items/unions.html

0 comments on commit b34bb93

Please sign in to comment.