diff --git a/nostarch/chapter06.md b/nostarch/chapter06.md index 3bdbe31ab..11a824321 100644 --- a/nostarch/chapter06.md +++ b/nostarch/chapter06.md @@ -1,17 +1,6 @@ [TOC] - - - # Enums In this chapter we'll look at *enumerations*, also referred to as *enums*. @@ -30,11 +19,6 @@ functional languages like F#, OCaml, or Haskell. ## Defining an Enum - - Let's look at a situation we might want to express in code and see why enums are useful and more appropriate than structs in this case. Say we need to work with IP addresses. There are two major standards used for IP addresses today: @@ -64,25 +48,6 @@ This is now a custom data type that we can use elsewhere in our code. ### Enum Values - - An `enum` definition is similar to a `struct` definition: it defines a new type and a template of what instances of that new type will be like. When you want to @@ -153,8 +118,6 @@ let loopback = IpAddr { Listing 6-1: Storing the data and type of an IP address using a `struct` - - Here, we've defined a struct `IPAddr` that has two fields: a `kind` field that is of type `IpAddrKind` (the enum we defined previously), and an `address` field of type `String`. We have two instances of this struct. The first, @@ -287,8 +250,6 @@ let m = Message::Write(String::from("hello")); m.call(); ``` - - The body of the method would use `self` to get the value that we called the method on. In this example, we've created a variable `m` that has the value `Message::Write("hello")`, and that is what `self` will be in the body of @@ -354,13 +315,6 @@ 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`. - - - - The `` syntax is a feature of Rust we haven't talked about yet. It's a generic type parameter, and we'll cover generics in more detail in Chapter 10. For now, all you need to know is that this means the `Some` variant of the @@ -426,11 +380,6 @@ when the value is null. Everywhere that a value has a type that isn't an deliberate design decision for Rust to limit null's pervasiveness and increase the safety of Rust code. - - - So, how _do_ you get the `T` value out of a `Some` variant when you have a value of type `Option` so that you can use that value? The `Option` enum has a large number of methods useful in a variety of situations that you can @@ -446,12 +395,6 @@ flow construct that does just this, when used with enums: it will run different code depending on which variant of the enum it has, and that code can use the data inside the matching value. - - - ## Match Rust has an extremely powerful control-flow operator, `match`, that allows us to @@ -494,13 +437,6 @@ Listing 6-2: An enum and a `match` expression that has the variants of the enum as its patterns. - - - 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 `coin`. This feels very similar to an expression used with `if`, but there's a @@ -640,8 +576,6 @@ let none = plus_one(None); Listing 6-4: A function that uses a `match` expression on an `Option` - - #### Matching `Some(T)` Let's examine the first execution of `plus_one` in more detail. In the above diff --git a/src/ch06-00-enums.md b/src/ch06-00-enums.md index 6170d6766..b0808a1ab 100644 --- a/src/ch06-00-enums.md +++ b/src/ch06-00-enums.md @@ -1,14 +1,3 @@ - - - # Enums In this chapter we'll look at *enumerations*, also referred to as *enums*. @@ -27,11 +16,6 @@ functional languages like F#, OCaml, or Haskell. ## Defining an Enum - - Let's look at a situation we might want to express in code and see why enums are sometimes a more appropriate choice than structs for modeling data. Say we need to work with IP addresses. There are two major standards used for IP addresses @@ -61,25 +45,6 @@ This is now a custom data type that we can use elsewhere in our code. ### Enum Values - - An `enum` definition is similar to a `struct` definition: it defines a new type and a template of what instances of that new type will be like. When you want to @@ -172,8 +137,6 @@ Listing 6-1: Storing the data and type of an IP address using a `struct` - - Here, we've defined a struct `IpAddr` that has two fields: a `kind` field that is of type `IpAddrKind` (the enum we defined previously), and an `address` field of type `String`. We have two instances of this struct. The first, @@ -316,8 +279,6 @@ let m = Message::Write(String::from("hello")); m.call(); ``` - - The body of the method would use `self` to get the value that we called the method on. In this example, we've created a variable `m` that has the value `Message::Write("hello")`, and that is what `self` will be in the body of diff --git a/src/ch06-01-option.md b/src/ch06-01-option.md index 0ff6f1ca3..c2448a2e8 100644 --- a/src/ch06-01-option.md +++ b/src/ch06-01-option.md @@ -57,13 +57,6 @@ use `Some` and `None` directly, without prefixing them with `Option::`. `Option` is still just a regular enum, however, and `Some(T)` and `None` are still values of type `Option`. - - - - The `` syntax is a feature of Rust we haven't talked about yet. It's a generic type parameter, and we'll cover generics in more detail in Chapter 10. For now, all you need to know is that this means the `Some` variant of the @@ -130,11 +123,6 @@ when the value is null. Everywhere that a value has a type that isn't an deliberate design decision for Rust to limit null's pervasiveness and increase the safety of Rust code. - - - So, how *do* you get the `T` value out of a `Some` variant when you have a value of type `Option` so that you can use that value? The `Option` enum has a large number of methods useful in a variety of situations that you can @@ -151,9 +139,3 @@ code doesn't have a `T` value available. The `match` expression is a control flow construct that does just this, when used with enums: it will run different code depending on which variant of the enum it has, and that code can use the data inside the matching value. - - - diff --git a/src/ch06-02-match.md b/src/ch06-02-match.md index b398ae1d2..7cb9fc7d4 100644 --- a/src/ch06-02-match.md +++ b/src/ch06-02-match.md @@ -45,13 +45,6 @@ as its patterns. - - - 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 `coin`. This feels very similar to an expression used with `if`, but there's a @@ -221,8 +214,6 @@ Listing 6-4: A function that uses a `match` expression on an `Option` - - #### Matching `Some(T)` Let's examine the first execution of `plus_one` in more detail. In the above