mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 19:24:32 -06:00
Minor rewordings on one last readthrough
This commit is contained in:
@@ -43,11 +43,11 @@ Any IP address can be either a version four or a version six address, but not
|
||||
both at the same time. That property of IP addresses makes the enum data
|
||||
structure appropriate for this case, since enum values can only be one of the
|
||||
variants. Both version four and version six addresses are still fundamentally
|
||||
IP addresses, though, so they should be treated as the same type when
|
||||
appropriate.
|
||||
IP addresses, though, so they should be treated as the same type when the code
|
||||
is handling situations that apply to any kind of IP address.
|
||||
|
||||
We can express this concept in code by defining an `IpAddrKind` enumeration and
|
||||
listing the possible kinds an IP address can be, `V4` and `V6`, which are known
|
||||
listing the possible kinds an IP address can be, `V4` and `V6`. These are known
|
||||
as the *variants* of the enum:
|
||||
|
||||
```rust
|
||||
@@ -137,9 +137,9 @@ route(IpAddrKind::V6);
|
||||
```
|
||||
|
||||
Enums have more tricks up their sleeves, too. Thinking more about our IP
|
||||
address type, at the moment we don’t have a way to store the actual *data* of the
|
||||
IP address; we only know what *kind* it is. Given that you just learned about
|
||||
structs, you might tackle this problem as in Listing 6-1:
|
||||
address type, at the moment we don’t have a way to store the actual *data* of
|
||||
the IP address; we only know what *kind* it is. Given that you just learned
|
||||
about structs, you might tackle this problem as in Listing 6-1:
|
||||
|
||||
```rust
|
||||
enum IpAddrKind {
|
||||
|
||||
+16
-16
@@ -3,17 +3,17 @@
|
||||
In the previous section, we looked at how the `IpAddr` enum let us use Rust's
|
||||
type system to encode more information than just the data into our program.
|
||||
This section is a case study of `Option`, which is another enum defined by the
|
||||
standard library that is used in many places because it encodes the very common
|
||||
scenario in which a value could be *something* or it could be *nothing*.
|
||||
Expressing this concept in terms of the type system means the compiler can
|
||||
check that we've handled all the cases we should be handling, which can prevent
|
||||
bugs that are extremely common in other programming languages.
|
||||
standard library. The `Option` type is used in many places because it encodes
|
||||
the very common scenario in which a value could be *something* or it could be
|
||||
*nothing*. Expressing this concept in terms of the type system means the
|
||||
compiler can check that you've handled all the cases you should be handling,
|
||||
which can prevent bugs that are extremely common in other programming languages.
|
||||
|
||||
Programming language design is often thought of in terms of which features you
|
||||
include, but the features you leave out are important too. Rust does not have
|
||||
the *null* feature that many other languages have. Null is a value that
|
||||
means there is no value there. In languages with null, variables
|
||||
can always be in one of two states: null or not-null.
|
||||
the *null* feature that many other languages have. Null is a value that means
|
||||
there is no value there. In languages with null, variables can always be in one
|
||||
of two states: null or not-null.
|
||||
|
||||
The inventor of null has this to say:
|
||||
|
||||
@@ -38,9 +38,9 @@ null is a value which is currently invalid or absent for some reason.
|
||||
|
||||
The problem isn't with the concept itself, but with the particular
|
||||
implementation. As such, Rust does not have nulls, but it does have an enum
|
||||
that can encode the concept of a value being present or absent. We call this
|
||||
enum `Option<T>`, and it is [defined by the standard
|
||||
library][option]<!-- ignore --> as follows:
|
||||
that can encode the concept of a value being present or absent. This enum is
|
||||
`Option<T>`, and it is [defined by the standard library][option]<!-- ignore -->
|
||||
as follows:
|
||||
|
||||
[option]: ../std/option/enum.Option.html
|
||||
|
||||
@@ -51,11 +51,11 @@ enum Option<T> {
|
||||
}
|
||||
```
|
||||
|
||||
This enum is so useful that it's even included in the prelude; you don't need
|
||||
to import it explicitly. Furthermore, so are its variants: you can use `Some`
|
||||
and `None` directly, without prefixing them with `Option::`. This is still just
|
||||
a regular enum, however, `Some(T)` and `None` are still values of type
|
||||
`Option<T>`.
|
||||
The `Option<T>` enum is so useful that it's even included in the prelude; you
|
||||
don't need to import it explicitly. Furthermore, so are its variants: you can
|
||||
use `Some` and `None` directly, without prefixing them with `Option::`. This is
|
||||
still just a regular enum, however, `Some(T)` and `None` are still values of
|
||||
type `Option<T>`.
|
||||
|
||||
<!-- We haven't spoken about the prelude so far in the book, I think I made a
|
||||
note of that in a previous chapter---we should tell the reader what it is
|
||||
|
||||
+21
-20
@@ -2,7 +2,8 @@
|
||||
|
||||
Rust has an extremely powerful control-flow operator, `match`, that allows us to
|
||||
compare a value against a series of patterns and then execute code based on
|
||||
which pattern matches.
|
||||
which pattern matches. The power comes from the expressiveness of the patterns
|
||||
and the compiler checks that make sure all possible cases are handled.
|
||||
|
||||
Think of a `match` expression kind of like a coin sorting machine: coins slide
|
||||
down a track with variously sized holes along it, and each coin falls
|
||||
@@ -44,7 +45,7 @@ put two arms in this example? I think that would illustrate the control flow
|
||||
well -->
|
||||
<!-- I think we're moving away from using generic examples like this and talking
|
||||
about concrete examples instead. I've changed the text to reflect that, and I'm
|
||||
happy to add wingdings once we're in libreoffice. -->
|
||||
happy to add wingdings once we're in libreoffice. /Carol -->
|
||||
|
||||
Let's break down the `match` in the `value_in_cents` function. First, we list
|
||||
the `match` keyword followed by an expression, which in this case is the value
|
||||
@@ -65,12 +66,12 @@ value, execution continues to the next arm, much like a coin sorting machine.
|
||||
We can have as many arms as we need: our `match` above has four arms.
|
||||
|
||||
The code associated with each arm is an expression, and the resulting value of
|
||||
the expression in the matching arm is the value that gets
|
||||
returned for the entire `match` expression.
|
||||
the expression in the matching arm is the value that gets returned for the
|
||||
entire `match` expression.
|
||||
|
||||
Curly braces typically aren't used if the match arm code is short, as it is in
|
||||
the above example where each arm just returns a value. If we wanted to run
|
||||
multiple lines of code in a match arm, we can use curly braces. For example,
|
||||
the above example where each arm just returns a value. If you wanted to run
|
||||
multiple lines of code in a match arm, you can use curly braces. For example,
|
||||
this code would print out "Lucky penny!" every time the method was called with
|
||||
a `Coin::Penny`, but would still return the last value of the block, `1`:
|
||||
|
||||
@@ -177,10 +178,10 @@ state value out of the `Coin` enum variant for `Quarter`.
|
||||
|
||||
In the previous section we wanted to get the inner `T` value out of the `Some`
|
||||
case when using `Option<T>`; we can do this in a very similar way! Instead of
|
||||
comparing coins we will be comparing patterns, but the way that the `match`
|
||||
expression works remains the same.
|
||||
comparing coins we will be comparing the variants of `Option<T>`, but the way
|
||||
that the `match` expression works remains the same.
|
||||
|
||||
Let's say we want to write a function that takes an `Option<i32>`, and if
|
||||
Let's say we want to write a function that takes an `Option<i32>` and if
|
||||
there's a value inside, adds one to that value. If there isn't a value inside,
|
||||
the function should return the `None` value and not attempt to perform any
|
||||
operations.
|
||||
@@ -210,8 +211,8 @@ Listing 6-4: A function that uses a `match` expression on an `Option<i32>`
|
||||
#### Matching `Some(T)`
|
||||
|
||||
Let's examine the first execution of `plus_one()` in more detail. In the above
|
||||
example when we call `plus_one(five)`, the variable `x` in the body
|
||||
of`plus_one` will have the value `Some(5)`. We compare that against each match
|
||||
example when we call `plus_one(five)`, the variable `x` in the body of
|
||||
`plus_one` will have the value `Some(5)`. We compare that against each match
|
||||
arm:
|
||||
|
||||
```rust,ignore
|
||||
@@ -273,11 +274,11 @@ error[E0004]: non-exhaustive patterns: `None` not covered
|
||||
```
|
||||
|
||||
Rust knows that we did not cover every possible option, and even knows which
|
||||
pattern we forgot! Enums in Rust are *exhaustive*: we must exhaust
|
||||
every last option possible in order to be valid. Especially in the case of
|
||||
`Option<T>`, when Rust prevents us from forgetting to explicitly handle the
|
||||
`None` case, it protects us from assuming that we have a value when we might
|
||||
have null and thus making the billion-dollar mistake discussed earlier.
|
||||
pattern we forgot! Enums in Rust are *exhaustive*: we must exhaust every last
|
||||
option possible in order to be valid. Especially in the case of `Option<T>`,
|
||||
when Rust prevents us from forgetting to explicitly handle the `None` case, it
|
||||
protects us from assuming that we have a value when we might have null and thus
|
||||
making the billion-dollar mistake discussed earlier.
|
||||
|
||||
### The _ Placeholder
|
||||
|
||||
@@ -299,10 +300,10 @@ match some_u8_value {
|
||||
```
|
||||
|
||||
The `_` pattern will match any value. By putting it after our other arms, the
|
||||
`_` will match all the possible cases that aren't specified before it. The `{}`
|
||||
is just an empty block of code, so nothing will happen in the `_` case. This
|
||||
way, we can say that we want to do nothing for all of the possible values that
|
||||
we don't list before the `_` placeholder.
|
||||
`_` will match all the possible cases that aren't specified before it. The `()`
|
||||
is just the unit value, so nothing will happen in the `_` case. This way, we
|
||||
can say that we want to do nothing for all of the possible values that we don't
|
||||
list before the `_` placeholder.
|
||||
|
||||
The `match` expression can be a little wordy for the case where we only care
|
||||
about *one* of the cases, though. For that case, Rust provides `if let`.
|
||||
|
||||
@@ -30,7 +30,7 @@ if let Some(x) = some_option {
|
||||
```
|
||||
|
||||
`if let` takes a pattern and an expression separated by an `=`. It works
|
||||
exactly like a `match`, where the expression is given to the `match` and the
|
||||
just like a `match`, where the expression is given to the `match` and the
|
||||
pattern is its first arm.
|
||||
|
||||
Using `if let` means you have less to type, less indentation, and less
|
||||
@@ -103,9 +103,9 @@ toolbox as well.
|
||||
|
||||
We've now covered how to use enums to create custom types that can be one of a
|
||||
set of enumerated values. We've shown how the standard library's `Option<T>`
|
||||
type helps us use the type system to prevent errors. When enum values have data
|
||||
inside them, we can use `match` or `if let` to extract and use those values,
|
||||
depending on how many cases we need to handle.
|
||||
type helps you use the type system to prevent errors. When enum values have data
|
||||
inside them, you can use `match` or `if let` to extract and use those values,
|
||||
depending on how many cases you need to handle.
|
||||
|
||||
Your Rust programs can now express concepts in your domain using structs and
|
||||
enums. Creating custom types to use in your API ensures type safety: the
|
||||
|
||||
Reference in New Issue
Block a user