mirror of
https://github.com/rust-lang/book.git
synced 2026-12-04 20:20:56 -06:00
Backport copyedit changes to Appendix C
This commit is contained in:
committed by
Carol (Nichols || Goulding)
parent
0846f81cab
commit
042803ac0f
+28
-27
@@ -371,9 +371,9 @@ library that you can use with `derive`. Each section covers:
|
||||
|
||||
If you want different behavior from that provided by the `derive` attribute,
|
||||
consult the standard library documentation
|
||||
for each trait for details of how to manually implement them.
|
||||
for each trait for details on how to manually implement them.
|
||||
|
||||
These traits listed here are the only ones defined by the standard library that
|
||||
The traits listed here are the only ones defined by the standard library that
|
||||
can be implemented on your types using `derive`. Other traits defined in the
|
||||
standard library don’t have sensible default behavior, so it’s up to you to
|
||||
implement them in the way that makes sense for what you’re trying to accomplish.
|
||||
@@ -386,10 +386,10 @@ would be most relevant to them? The Rust compiler doesn’t have this insight, s
|
||||
it can’t provide appropriate default behavior for you.
|
||||
|
||||
The list of derivable traits provided in this appendix is not comprehensive:
|
||||
libraries can implement `derive` for their own traits, making the list of
|
||||
traits you can use `derive` with truly open-ended. Implementing `derive`
|
||||
involves using a procedural macro, which is covered in the
|
||||
“Macros” section of Chapter 20.
|
||||
Libraries can implement `derive` for their own traits, making the list of
|
||||
traits you can use `derive` with truly open ended. Implementing `derive`
|
||||
involves using a procedural macro, which is covered in the “Custom `derive`
|
||||
Macros” section in Chapter 20.
|
||||
|
||||
### Debug for Programmer Output
|
||||
|
||||
@@ -400,9 +400,10 @@ The `Debug` trait allows you to print instances of a type for debugging
|
||||
purposes, so you and other programmers using your type can inspect an instance
|
||||
at a particular point in a program’s execution.
|
||||
|
||||
The `Debug` trait is required, for example, in using the `assert_eq!` macro.
|
||||
This macro prints the values of instances given as arguments if the equality
|
||||
assertion fails so programmers can see why the two instances weren’t equal.
|
||||
The `Debug` trait is required, for example, in the use of the `assert_eq!`
|
||||
macro. This macro prints the values of instances given as arguments if the
|
||||
equality assertion fails so that programmers can see why the two instances
|
||||
weren’t equal.
|
||||
|
||||
### PartialEq and Eq for Equality Comparisons
|
||||
|
||||
@@ -411,7 +412,7 @@ equality and enables use of the `==` and `!=` operators.
|
||||
|
||||
Deriving `PartialEq` implements the `eq` method. When `PartialEq` is derived on
|
||||
structs, two instances are equal only if *all* fields are equal, and the
|
||||
instances are not equal if any fields are not equal. When derived on enums,
|
||||
instances are not equal if *any* fields are not equal. When derived on enums,
|
||||
each variant is equal to itself and not equal to the other variants.
|
||||
|
||||
The `PartialEq` trait is required, for example, with the use of the
|
||||
@@ -421,12 +422,12 @@ for equality.
|
||||
The `Eq` trait has no methods. Its purpose is to signal that for every value of
|
||||
the annotated type, the value is equal to itself. The `Eq` trait can only be
|
||||
applied to types that also implement `PartialEq`, although not all types that
|
||||
implement `PartialEq` can implement `Eq`. One example of this is floating point
|
||||
number types: the implementation of floating point numbers states that two
|
||||
implement `PartialEq` can implement `Eq`. One example of this is floating-point
|
||||
number types: The implementation of floating-point numbers states that two
|
||||
instances of the not-a-number (`NaN`) value are not equal to each other.
|
||||
|
||||
An example of when `Eq` is required is for keys in a `HashMap<K, V>` so the
|
||||
`HashMap<K, V>` can tell whether two keys are the same.
|
||||
An example of when `Eq` is required is for keys in a `HashMap<K, V>` so that
|
||||
the `HashMap<K, V>` can tell whether two keys are the same.
|
||||
|
||||
### PartialOrd and Ord for Ordering Comparisons
|
||||
|
||||
@@ -439,8 +440,8 @@ Deriving `PartialOrd` implements the `partial_cmp` method, which returns an
|
||||
`Option<Ordering>` that will be `None` when the values given don’t produce an
|
||||
ordering. An example of a value that doesn’t produce an ordering, even though
|
||||
most values of that type can be compared, is the `NaN` floating point value.
|
||||
Calling `partial_cmp` with any floating point number and the `NaN` floating
|
||||
point value will return `None`.
|
||||
Calling `partial_cmp` with any floating-point number and the `NaN`
|
||||
floating-point value will return `None`.
|
||||
|
||||
When derived on structs, `PartialOrd` compares two instances by comparing the
|
||||
value in each field in the order in which the fields appear in the struct
|
||||
@@ -466,9 +467,9 @@ a data structure that stores data based on the sort order of the values.
|
||||
|
||||
The `Clone` trait allows you to explicitly create a deep copy of a value, and
|
||||
the duplication process might involve running arbitrary code and copying heap
|
||||
data. See Variables and Data Interacting with
|
||||
Clone” in Chapter 4
|
||||
for more information on `Clone`.
|
||||
data. See the “Variables and Data Interacting with
|
||||
Clone” section in
|
||||
Chapter 4 for more information on `Clone`.
|
||||
|
||||
Deriving `Clone` implements the `clone` method, which when implemented for the
|
||||
whole type, calls `clone` on each of the parts of the type. This means all the
|
||||
@@ -480,9 +481,9 @@ returned from `to_vec` will need to own its instances, so `to_vec` calls
|
||||
`clone` on each item. Thus, the type stored in the slice must implement `Clone`.
|
||||
|
||||
The `Copy` trait allows you to duplicate a value by only copying bits stored on
|
||||
the stack; no arbitrary code is necessary. See “Stack-Only Data:
|
||||
Copy” in Chapter 4 for more information on
|
||||
`Copy`.
|
||||
the stack; no arbitrary code is necessary. See the “Stack-Only Data:
|
||||
Copy” section in Chapter 4 for more
|
||||
information on `Copy`.
|
||||
|
||||
The `Copy` trait doesn’t define any methods to prevent programmers from
|
||||
overloading those methods and violating the assumption that no arbitrary code
|
||||
@@ -490,7 +491,7 @@ is being run. That way, all programmers can assume that copying a value will be
|
||||
very fast.
|
||||
|
||||
You can derive `Copy` on any type whose parts all implement `Copy`. A type that
|
||||
implements `Copy` must also implement `Clone`, because a type that implements
|
||||
implements `Copy` must also implement `Clone` because a type that implements
|
||||
`Copy` has a trivial implementation of `Clone` that performs the same task as
|
||||
`Copy`.
|
||||
|
||||
@@ -521,10 +522,10 @@ meaning all fields or values in the type must also implement `Default` to
|
||||
derive `Default`.
|
||||
|
||||
The `Default::default` function is commonly used in combination with the struct
|
||||
update syntax discussed in “Creating Instances From Other Instances With Struct
|
||||
Update
|
||||
Syntax” in Chapter 5. You can customize a few fields of a struct and then set
|
||||
and use a default value for the rest of the fields by using
|
||||
update syntax discussed in the “Creating Instances from Other Instances with
|
||||
Struct Update
|
||||
Syntax” section in Chapter 5. You can customize a few fields of a struct and
|
||||
then set and use a default value for the rest of the fields by using
|
||||
`..Default::default()`.
|
||||
|
||||
The `Default` trait is required when you use the method `unwrap_or_default` on
|
||||
|
||||
+17
-16
@@ -39,9 +39,10 @@ would be most relevant to them? The Rust compiler doesn’t have this insight, s
|
||||
it can’t provide appropriate default behavior for you.
|
||||
|
||||
The list of derivable traits provided in this appendix is not comprehensive:
|
||||
libraries can implement `derive` for their own traits, making the list of
|
||||
Libraries can implement `derive` for their own traits, making the list of
|
||||
traits you can use `derive` with truly open ended. Implementing `derive`
|
||||
involves using a procedural macro, which is covered in “Macros” on page XX.
|
||||
involves using a procedural macro, which is covered in the “Custom `derive`
|
||||
Macros” section in Chapter 20.
|
||||
|
||||
## Debug for Programmer Output
|
||||
|
||||
@@ -54,8 +55,8 @@ at a particular point in a program’s execution.
|
||||
|
||||
The `Debug` trait is required, for example, in the use of the `assert_eq!`
|
||||
macro. This macro prints the values of instances given as arguments if the
|
||||
equality assertion fails so programmers can see why the two instances weren’t
|
||||
equal.
|
||||
equality assertion fails so that programmers can see why the two instances
|
||||
weren’t equal.
|
||||
|
||||
## PartialEq and Eq for Equality Comparisons
|
||||
|
||||
@@ -64,7 +65,7 @@ equality and enables use of the `==` and `!=` operators.
|
||||
|
||||
Deriving `PartialEq` implements the `eq` method. When `PartialEq` is derived on
|
||||
structs, two instances are equal only if *all* fields are equal, and the
|
||||
instances are not equal if any fields are not equal. When derived on enums,
|
||||
instances are not equal if *any* fields are not equal. When derived on enums,
|
||||
each variant is equal to itself and not equal to the other variants.
|
||||
|
||||
The `PartialEq` trait is required, for example, with the use of the
|
||||
@@ -75,7 +76,7 @@ The `Eq` trait has no methods. Its purpose is to signal that for every value of
|
||||
the annotated type, the value is equal to itself. The `Eq` trait can only be
|
||||
applied to types that also implement `PartialEq`, although not all types that
|
||||
implement `PartialEq` can implement `Eq`. One example of this is floating-point
|
||||
number types: the implementation of floating-point numbers states that two
|
||||
number types: The implementation of floating-point numbers states that two
|
||||
instances of the not-a-number (`NaN`) value are not equal to each other.
|
||||
|
||||
An example of when `Eq` is required is for keys in a `HashMap<K, V>` so that
|
||||
@@ -91,8 +92,8 @@ that also implement `PartialEq`.
|
||||
Deriving `PartialOrd` implements the `partial_cmp` method, which returns an
|
||||
`Option<Ordering>` that will be `None` when the values given don’t produce an
|
||||
ordering. An example of a value that doesn’t produce an ordering, even though
|
||||
most values of that type can be compared, is the not-a-number (`NaN`) floating
|
||||
point value. Calling `partial_cmp` with any floating-point number and the `NaN`
|
||||
most values of that type can be compared, is the `NaN` floating point value.
|
||||
Calling `partial_cmp` with any floating-point number and the `NaN`
|
||||
floating-point value will return `None`.
|
||||
|
||||
When derived on structs, `PartialOrd` compares two instances by comparing the
|
||||
@@ -119,8 +120,8 @@ a data structure that stores data based on the sort order of the values.
|
||||
|
||||
The `Clone` trait allows you to explicitly create a deep copy of a value, and
|
||||
the duplication process might involve running arbitrary code and copying heap
|
||||
data. See “Variables and Data Interacting with Clone” on page XX for more
|
||||
information on `Clone`.
|
||||
data. See the “Variables and Data Interacting with Clone” section in Chapter 4
|
||||
for more information on `Clone`.
|
||||
|
||||
Deriving `Clone` implements the `clone` method, which when implemented for the
|
||||
whole type, calls `clone` on each of the parts of the type. This means all the
|
||||
@@ -129,11 +130,11 @@ fields or values in the type must also implement `Clone` to derive `Clone`.
|
||||
An example of when `Clone` is required is when calling the `to_vec` method on a
|
||||
slice. The slice doesn’t own the type instances it contains, but the vector
|
||||
returned from `to_vec` will need to own its instances, so `to_vec` calls
|
||||
`clone` on each item. Thus the type stored in the slice must implement `Clone`.
|
||||
`clone` on each item. Thus, the type stored in the slice must implement `Clone`.
|
||||
|
||||
The `Copy` trait allows you to duplicate a value by only copying bits stored on
|
||||
the stack; no arbitrary code is necessary. See “Stack-Only Data: Copy” on page
|
||||
XX for more information on `Copy`.
|
||||
the stack; no arbitrary code is necessary. See the “Stack-Only Data: Copy”
|
||||
section in Chapter 4 for more information on `Copy`.
|
||||
|
||||
The `Copy` trait doesn’t define any methods to prevent programmers from
|
||||
overloading those methods and violating the assumption that no arbitrary code
|
||||
@@ -172,9 +173,9 @@ meaning all fields or values in the type must also implement `Default` to
|
||||
derive `Default`.
|
||||
|
||||
The `Default::default` function is commonly used in combination with the struct
|
||||
update syntax discussed in “Creating Instances from Other Instances with Struct
|
||||
Update Syntax” on page XX. You can customize a few fields of a struct and then
|
||||
set and use a default value for the rest of the fields by using
|
||||
update syntax discussed in the “Creating Instances with Struct Update Syntax”
|
||||
section in Chapter 5. You can customize a few fields of a struct and then set
|
||||
and use a default value for the rest of the fields by using
|
||||
`..Default::default()`.
|
||||
|
||||
The `Default` trait is required when you use the method `unwrap_or_default` on
|
||||
|
||||
Binary file not shown.
@@ -31,10 +31,10 @@ would be most relevant to them? The Rust compiler doesn’t have this insight, s
|
||||
it can’t provide appropriate default behavior for you.
|
||||
|
||||
The list of derivable traits provided in this appendix is not comprehensive:
|
||||
libraries can implement `derive` for their own traits, making the list of
|
||||
traits you can use `derive` with truly open-ended. Implementing `derive`
|
||||
involves using a procedural macro, which is covered in the
|
||||
[“Macros”][macros]<!-- ignore --> section of Chapter 20.
|
||||
Libraries can implement `derive` for their own traits, making the list of
|
||||
traits you can use `derive` with truly open ended. Implementing `derive`
|
||||
involves using a procedural macro, which is covered in the [“Custom `derive`
|
||||
Macros”][custom-derive-macros]<!-- ignore --> section in Chapter 20.
|
||||
|
||||
### `Debug` for Programmer Output
|
||||
|
||||
@@ -47,8 +47,8 @@ at a particular point in a program’s execution.
|
||||
|
||||
The `Debug` trait is required, for example, in the use of the `assert_eq!`
|
||||
macro. This macro prints the values of instances given as arguments if the
|
||||
equality assertion fails so programmers can see why the two instances weren’t
|
||||
equal.
|
||||
equality assertion fails so that programmers can see why the two instances
|
||||
weren’t equal.
|
||||
|
||||
### `PartialEq` and `Eq` for Equality Comparisons
|
||||
|
||||
@@ -57,7 +57,7 @@ equality and enables use of the `==` and `!=` operators.
|
||||
|
||||
Deriving `PartialEq` implements the `eq` method. When `PartialEq` is derived on
|
||||
structs, two instances are equal only if _all_ fields are equal, and the
|
||||
instances are not equal if any fields are not equal. When derived on enums,
|
||||
instances are not equal if _any_ fields are not equal. When derived on enums,
|
||||
each variant is equal to itself and not equal to the other variants.
|
||||
|
||||
The `PartialEq` trait is required, for example, with the use of the
|
||||
@@ -67,12 +67,12 @@ for equality.
|
||||
The `Eq` trait has no methods. Its purpose is to signal that for every value of
|
||||
the annotated type, the value is equal to itself. The `Eq` trait can only be
|
||||
applied to types that also implement `PartialEq`, although not all types that
|
||||
implement `PartialEq` can implement `Eq`. One example of this is floating point
|
||||
number types: the implementation of floating point numbers states that two
|
||||
implement `PartialEq` can implement `Eq`. One example of this is floating-point
|
||||
number types: The implementation of floating-point numbers states that two
|
||||
instances of the not-a-number (`NaN`) value are not equal to each other.
|
||||
|
||||
An example of when `Eq` is required is for keys in a `HashMap<K, V>` so the
|
||||
`HashMap<K, V>` can tell whether two keys are the same.
|
||||
An example of when `Eq` is required is for keys in a `HashMap<K, V>` so that
|
||||
the `HashMap<K, V>` can tell whether two keys are the same.
|
||||
|
||||
### `PartialOrd` and `Ord` for Ordering Comparisons
|
||||
|
||||
@@ -84,8 +84,8 @@ that also implement `PartialEq`.
|
||||
Deriving `PartialOrd` implements the `partial_cmp` method, which returns an
|
||||
`Option<Ordering>` that will be `None` when the values given don’t produce an
|
||||
ordering. An example of a value that doesn’t produce an ordering, even though
|
||||
most values of that type can be compared, is the not-a-number (`NaN`) floating
|
||||
point value. Calling `partial_cmp` with any floating-point number and the `NaN`
|
||||
most values of that type can be compared, is the `NaN` floating point value.
|
||||
Calling `partial_cmp` with any floating-point number and the `NaN`
|
||||
floating-point value will return `None`.
|
||||
|
||||
When derived on structs, `PartialOrd` compares two instances by comparing the
|
||||
@@ -112,9 +112,9 @@ a data structure that stores data based on the sort order of the values.
|
||||
|
||||
The `Clone` trait allows you to explicitly create a deep copy of a value, and
|
||||
the duplication process might involve running arbitrary code and copying heap
|
||||
data. See [Variables and Data Interacting with
|
||||
Clone”][variables-and-data-interacting-with-clone]<!-- ignore --> in Chapter 4
|
||||
for more information on `Clone`.
|
||||
data. See the [“Variables and Data Interacting with
|
||||
Clone”][variables-and-data-interacting-with-clone]<!-- ignore --> section in
|
||||
Chapter 4 for more information on `Clone`.
|
||||
|
||||
Deriving `Clone` implements the `clone` method, which when implemented for the
|
||||
whole type, calls `clone` on each of the parts of the type. This means all the
|
||||
@@ -123,12 +123,12 @@ fields or values in the type must also implement `Clone` to derive `Clone`.
|
||||
An example of when `Clone` is required is when calling the `to_vec` method on a
|
||||
slice. The slice doesn’t own the type instances it contains, but the vector
|
||||
returned from `to_vec` will need to own its instances, so `to_vec` calls
|
||||
`clone` on each item. Thus the type stored in the slice must implement `Clone`.
|
||||
`clone` on each item. Thus, the type stored in the slice must implement `Clone`.
|
||||
|
||||
The `Copy` trait allows you to duplicate a value by only copying bits stored on
|
||||
the stack; no arbitrary code is necessary. See [“Stack-Only Data:
|
||||
Copy”][stack-only-data-copy]<!-- ignore --> in Chapter 4 for more information on
|
||||
`Copy`.
|
||||
the stack; no arbitrary code is necessary. See the [“Stack-Only Data:
|
||||
Copy”][stack-only-data-copy]<!-- ignore --> section in Chapter 4 for more
|
||||
information on `Copy`.
|
||||
|
||||
The `Copy` trait doesn’t define any methods to prevent programmers from
|
||||
overloading those methods and violating the assumption that no arbitrary code
|
||||
@@ -136,7 +136,7 @@ is being run. That way, all programmers can assume that copying a value will be
|
||||
very fast.
|
||||
|
||||
You can derive `Copy` on any type whose parts all implement `Copy`. A type that
|
||||
implements `Copy` must also implement `Clone`, because a type that implements
|
||||
implements `Copy` must also implement `Clone` because a type that implements
|
||||
`Copy` has a trivial implementation of `Clone` that performs the same task as
|
||||
`Copy`.
|
||||
|
||||
@@ -167,11 +167,11 @@ meaning all fields or values in the type must also implement `Default` to
|
||||
derive `Default`.
|
||||
|
||||
The `Default::default` function is commonly used in combination with the struct
|
||||
update syntax discussed in [“Creating Instances from Other Instances with Struct
|
||||
Update
|
||||
update syntax discussed in the [“Creating Instances from Other Instances with
|
||||
Struct Update
|
||||
Syntax”][creating-instances-from-other-instances-with-struct-update-syntax]<!--
|
||||
ignore --> in Chapter 5. You can customize a few fields of a struct and then set
|
||||
and use a default value for the rest of the fields by using
|
||||
ignore --> section in Chapter 5. You can customize a few fields of a struct and
|
||||
then set and use a default value for the rest of the fields by using
|
||||
`..Default::default()`.
|
||||
|
||||
The `Default` trait is required when you use the method `unwrap_or_default` on
|
||||
@@ -182,4 +182,4 @@ The `Default` trait is required when you use the method `unwrap_or_default` on
|
||||
[creating-instances-from-other-instances-with-struct-update-syntax]: ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax
|
||||
[stack-only-data-copy]: ch04-01-what-is-ownership.html#stack-only-data-copy
|
||||
[variables-and-data-interacting-with-clone]: ch04-01-what-is-ownership.html#variables-and-data-interacting-with-clone
|
||||
[macros]: ch20-05-macros.html#macros
|
||||
[custom-derive-macros]: ch20-05-macros.html#custom-derive-macros
|
||||
|
||||
Reference in New Issue
Block a user