mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 19:34:32 -06:00
Punctuation edits
This commit is contained in:
@@ -16,8 +16,8 @@ enum IpAddrKind {
|
||||
```
|
||||
|
||||
This enum represents the kind of an IP address. There are two major standards
|
||||
used for IP addresses: version four, and version six. Any IP address can be either
|
||||
a version four address, or a version six address. But it cannot be both kinds at
|
||||
used for IP addresses: version four and version six. Any IP address can be either
|
||||
a version four address or a version six address, but it cannot be both kinds at
|
||||
the same time. This is where enums get their name: they allow us to enumerate all
|
||||
of the possible kinds that our value can have.
|
||||
|
||||
@@ -37,7 +37,7 @@ Note that the variants of the enum are namespaced under its name, and we use
|
||||
the double colon to separate the two.
|
||||
|
||||
Enums have more tricks up their sleeves, however. Thinking more about our IP
|
||||
address type, we don’t have a way to store the actual data of the IP address,
|
||||
address type, 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 like this:
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ shows one of the big advantages of an `Option<T>` type: if you have a type that
|
||||
may or may not exist, you have to deal with that fact before you can assume it
|
||||
exists. In other words, you have to convert an `Option<T>` to a `T` before you
|
||||
can do `T` stuff with it. This helps catch one of the most common issues with
|
||||
null, generally: assuming that something isn't null, when it actually is.
|
||||
null, generally: assuming that something isn't null when it actually is.
|
||||
|
||||
So, how _do_ you get a `T` from an `Option<T>`? The option type has a large
|
||||
number of methods that you can check out in [its documentation], and becoming
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Match
|
||||
|
||||
Rust has an extremely powerful control-flow operator: `match`. It allows us to
|
||||
compare a value against a series of patterns, and then execute code based on
|
||||
compare a value against a series of patterns and then execute code based on
|
||||
how they compare. Remember the `Option<T>` type from the previous section?
|
||||
Let's say that we want to write a function that takes an `Option<i32>`, and
|
||||
if there's a value inside, add one to it.
|
||||
@@ -66,10 +66,10 @@ sum inside.
|
||||
|
||||
Because `match` is an expression, the value of the overall expression becomes
|
||||
the value of the arm that executed. So the value of this `match` expression
|
||||
will be `Some(6)`. And since our `match` is the only expression in the
|
||||
function, the value of the `match` will be the value of the function, and so
|
||||
`Some(6)` is our return value as well, which is exactly what we were shooting
|
||||
for.
|
||||
will be `Some(6)`, and since our `match` is the only expression in the
|
||||
function, the value of the `match` will be the value of the function. So
|
||||
`Some(6)` is our return value as well, which is exactly what we were trying
|
||||
to accomplish.
|
||||
|
||||
Now let's consider the second call. In this case, `x` is `None`. We enter the
|
||||
`match`, and compare to the first arm:
|
||||
@@ -111,7 +111,7 @@ match x {
|
||||
```
|
||||
|
||||
Rust knows that we did not cover every possible option, and even knows which
|
||||
pattern we forgot! This is referred to as being "exhaustive", we must exhaust
|
||||
pattern we forgot! This is referred to as being "exhaustive": we must exhaust
|
||||
every last option possible in order to be valid!
|
||||
|
||||
This analysis isn't perfect, however. This will also error:
|
||||
|
||||
@@ -18,7 +18,7 @@ case. With an `Option`, this isn't _too_ bad, but with a more complex enum,
|
||||
adding `_ => {}` after processing just one variant doesn't feel great. We have
|
||||
this boilerplate arm, and we have an extra level of indentation: the code that
|
||||
does something with `x` is indented twice, rather than just once. We really want
|
||||
a construct that says "Do something with this one case, I don't care about the
|
||||
a construct that says "Do something with this one case; I don't care about the
|
||||
others."
|
||||
|
||||
Enter `if let`:
|
||||
|
||||
Reference in New Issue
Block a user