mirror of https://github.com/rust-lang/book
Propagate chapter 15 tech review edits to src
This commit is contained in:
parent
85a94d95c0
commit
5041156975
|
@ -1580,7 +1580,7 @@ access to its interior mutability so we can modify our data when we need to.
|
|||
The runtime checks of the borrowing rules protect us from data races, and it’s
|
||||
sometimes worth trading a bit of speed for this flexibility in our data
|
||||
structures. Note that `RefCell<T>` does not work for multithreaded code!
|
||||
`Mutex<T>` is the threadsafe version of `RefCell<T>` and we’ll discuss
|
||||
`Mutex<T>` is the thread-safe version of `RefCell<T>` and we’ll discuss
|
||||
`Mutex<T>` in Chapter 16.
|
||||
|
||||
<!-- While we mention threading a little bit, I think we should take a little
|
||||
|
|
|
@ -76,10 +76,11 @@ more complex situations involving recursive types.
|
|||
#### More Information About the Cons List
|
||||
|
||||
A *cons list* is a data structure that comes from the Lisp programming language
|
||||
and its dialects and is made up of nested pairs. Its name comes from the `cons`
|
||||
function (short for “construct function”) in Lisp that constructs a new pair
|
||||
from its two arguments. By calling `cons` on a pair consisting of a value and
|
||||
another pair, we can construct cons lists made up of recursive pairs.
|
||||
and its dialects and is made up of nested pairs, and is the Lisp version of a
|
||||
linked list. Its name comes from the `cons` function (short for “construct
|
||||
function”) in Lisp that constructs a new pair from its two arguments. By
|
||||
calling `cons` on a pair consisting of a value and another pair, we can
|
||||
construct cons lists made up of recursive pairs.
|
||||
|
||||
For example, here's a pseudocode representation of a cons list containing the
|
||||
list 1, 2, 3 with each pair in parentheses:
|
||||
|
|
|
@ -192,7 +192,7 @@ Listing 15-9.
|
|||
*Deref coercion* converts a reference to a type that implements the `Deref`
|
||||
trait into a reference to another type. For example, deref coercion can convert
|
||||
`&String` to `&str` because `String` implements the `Deref` trait such that it
|
||||
returns `&str`. Deref conversion is a convenience Rust performs on arguments to
|
||||
returns `&str`. Deref coercion is a convenience Rust performs on arguments to
|
||||
functions and methods, and works only on types that implement the `Deref`
|
||||
trait. It happens automatically when we pass a reference to a particular type’s
|
||||
value as an argument to a function or method that doesn’t match the parameter
|
||||
|
|
|
@ -4,13 +4,14 @@
|
|||
data even when there are immutable references to that data; normally, this
|
||||
action is disallowed by the borrowing rules. To mutate data, the pattern uses
|
||||
`unsafe` code inside a data structure to bend Rust’s usual rules that govern
|
||||
mutation and borrowing. We haven’t yet covered unsafe code that indicates we're
|
||||
checking the rules manually instead of the compiler checking them for us; we
|
||||
will discuss unsafe code more in Chapter 19. We can use types that use the
|
||||
interior mutability pattern only when we can ensure that the borrowing rules
|
||||
will be followed at runtime, even though the compiler can’t guarantee that. The
|
||||
`unsafe` code involved is then wrapped in a safe API, and the outer type is
|
||||
still immutable.
|
||||
mutation and borrowing. mutation and borrowing. Unsafe code indicates to the
|
||||
compiler that we’re checking the rules manually instead of relying on the
|
||||
compiler to check them for us; we will discuss unsafe code more in Chapter 19.
|
||||
|
||||
We can use types that use the interior mutability pattern only when we can
|
||||
ensure that the borrowing rules will be followed at runtime, even though the
|
||||
compiler can’t guarantee that. The `unsafe` code involved is then wrapped in a
|
||||
safe API, and the outer type is still immutable.
|
||||
|
||||
Let’s explore this concept by looking at the `RefCell<T>` type that follows the
|
||||
interior mutability pattern.
|
||||
|
@ -336,13 +337,8 @@ immutable `List` value. But we can use the methods on `RefCell<T>` that provide
|
|||
access to its interior mutability so we can modify our data when we need to.
|
||||
The runtime checks of the borrowing rules protect us from data races, and it’s
|
||||
sometimes worth trading a bit of speed for this flexibility in our data
|
||||
structures.
|
||||
|
||||
The standard library has other types that provide interior mutability, such as
|
||||
`Cell<T>`, which is similar except that instead of giving references to the
|
||||
inner value, the value is copied in and out of the `Cell<T>`. There’s also
|
||||
`Mutex<T>`, which offers interior mutability that’s safe to use across threads;
|
||||
we’ll discuss its use in Chapter 16. Check out the standard library docs for
|
||||
more details on the differences between these types.
|
||||
structures. Note that `RefCell<T>` does not work for multithreaded code!
|
||||
`Mutex<T>` is the thread-safe version of `RefCell<T>` and we’ll discuss
|
||||
`Mutex<T>` in Chapter 16.
|
||||
|
||||
[wheres-the---operator]: ch05-03-method-syntax.html#wheres-the---operator
|
||||
|
|
Loading…
Reference in New Issue