From ed53e89cbdeb722403eeb79636fcff31a32c866b Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Thu, 9 Mar 2017 18:34:25 +0000 Subject: [PATCH] rebuild pages at 85b2c9a --- second-edition/ch15-01-box.html | 39 ++++++++++++++++++++++----------- second-edition/print.html | 39 ++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/second-edition/ch15-01-box.html b/second-edition/ch15-01-box.html index e6d35d0fe..43d45d416 100644 --- a/second-edition/ch15-01-box.html +++ b/second-edition/ch15-01-box.html @@ -87,18 +87,10 @@ has ownership of data, when a box goes out of scope like b does at (stored on the stack) and the data it points to (stored on the heap).

Putting a single value on the heap isn't very useful, so you won't use boxes by themselves in the way that Listing 15-1 does very often. A time when boxes are -useful is when you want to ensure that your type has a known size. For -example, consider Listing 15-2, which contains an enum definition for a cons -list, a type of data structure that comes from functional programming.

-

A cons list is a list where each item contains a value and the next item until -the end of the list, which is signified by a value called Nil. Note that we -aren't introducing the idea of "nil" or "null" that we discussed in Chapter 6, -this is just a regular enum variant name we're using because it's the canonical -name to use when describing the cons list data structure. Cons lists aren't -used very often in Rust, Vec<T> is a better choice most of the time, but -implementing this data structure is useful as an example.

-

Here's our first try at defining a cons list as an enum; note that this won't -compile quite yet:

+useful is when you want to ensure that your type has a known size. For example, +consider Listing 15-2, which contains an enum definition for a cons list, a +type of data structure that comes from functional programming. Note that this +won't compile quite yet:

Filename: src/main.rs

enum List {
     Cons(i32, List),
@@ -110,6 +102,27 @@ represent a cons list data structure of i32 values

We're implementing a cons list that holds only i32 values. We could have also chosen to implement a cons list independent of the type of value by using generics as discussed in Chapter 10.

+
+

More Information About the Cons List

+

A cons list is a data structure that comes from the Lisp programming +language and its dialects. In Lisp, the cons function (short for "construct +function") constructs a new list from its two arguments, which usually are a +single value and another list.

+

The cons function concept has made its way into more general functional +programming jargon; "to cons x onto y" informally means to construct a new +container instance by putting the element x at the start of this new +container, followed by the container y.

+

A cons list is produced by recursively calling the cons function. +The canonical name to denote the base case of the recursion is Nil, which +announces the end of the list. Note that this is not the same as the "null" +or "nil" concept from Chapter 6, which is an invalid or absent value.

+
+

A cons list is a list where each element contains both a single value as well +as the remains of the list at that point. The remains of the list are defined +by nested cons lists. The end of the list is signified by the value Nil. Cons +lists aren't used very often in Rust; Vec<T> is usually a better choice. +Implementing this data structure is a good example of a situation where +Box<T> is useful, though. Let's find out why!

Using a cons list to store the list 1, 2, 3 would look like this:

use List::{Cons, Nil};
 
@@ -158,7 +171,7 @@ forth. Therefore, the most space a Message value will need is the s
 would take to store the largest of its variants.

Contrast this to what happens when the Rust compiler looks at a recursive type like List in Listing 15-2. The compiler tries to figure out how much memory -is needed to store value of List, and starts by looking at the Cons +is needed to store a value of the List enum, and starts by looking at the Cons variant. The Cons variant holds a value of type i32 and a value of type List, so Cons needs an amount of space equal to the size of an i32 plus the size of a List. To figure out how much memory a List needs, it looks at diff --git a/second-edition/print.html b/second-edition/print.html index caf0f9317..ea34fe0d3 100644 --- a/second-edition/print.html +++ b/second-edition/print.html @@ -11550,18 +11550,10 @@ has ownership of data, when a box goes out of scope like b does at (stored on the stack) and the data it points to (stored on the heap).

Putting a single value on the heap isn't very useful, so you won't use boxes by themselves in the way that Listing 15-1 does very often. A time when boxes are -useful is when you want to ensure that your type has a known size. For -example, consider Listing 15-2, which contains an enum definition for a cons -list, a type of data structure that comes from functional programming.

-

A cons list is a list where each item contains a value and the next item until -the end of the list, which is signified by a value called Nil. Note that we -aren't introducing the idea of "nil" or "null" that we discussed in Chapter 6, -this is just a regular enum variant name we're using because it's the canonical -name to use when describing the cons list data structure. Cons lists aren't -used very often in Rust, Vec<T> is a better choice most of the time, but -implementing this data structure is useful as an example.

-

Here's our first try at defining a cons list as an enum; note that this won't -compile quite yet:

+useful is when you want to ensure that your type has a known size. For example, +consider Listing 15-2, which contains an enum definition for a cons list, a +type of data structure that comes from functional programming. Note that this +won't compile quite yet:

Filename: src/main.rs

enum List {
     Cons(i32, List),
@@ -11573,6 +11565,27 @@ represent a cons list data structure of i32 values

We're implementing a cons list that holds only i32 values. We could have also chosen to implement a cons list independent of the type of value by using generics as discussed in Chapter 10.

+
+

More Information About the Cons List

+

A cons list is a data structure that comes from the Lisp programming +language and its dialects. In Lisp, the cons function (short for "construct +function") constructs a new list from its two arguments, which usually are a +single value and another list.

+

The cons function concept has made its way into more general functional +programming jargon; "to cons x onto y" informally means to construct a new +container instance by putting the element x at the start of this new +container, followed by the container y.

+

A cons list is produced by recursively calling the cons function. +The canonical name to denote the base case of the recursion is Nil, which +announces the end of the list. Note that this is not the same as the "null" +or "nil" concept from Chapter 6, which is an invalid or absent value.

+
+

A cons list is a list where each element contains both a single value as well +as the remains of the list at that point. The remains of the list are defined +by nested cons lists. The end of the list is signified by the value Nil. Cons +lists aren't used very often in Rust; Vec<T> is usually a better choice. +Implementing this data structure is a good example of a situation where +Box<T> is useful, though. Let's find out why!

Using a cons list to store the list 1, 2, 3 would look like this:

use List::{Cons, Nil};
 
@@ -11621,7 +11634,7 @@ forth. Therefore, the most space a Message value will need is the s
 would take to store the largest of its variants.

Contrast this to what happens when the Rust compiler looks at a recursive type like List in Listing 15-2. The compiler tries to figure out how much memory -is needed to store value of List, and starts by looking at the Cons +is needed to store a value of the List enum, and starts by looking at the Cons variant. The Cons variant holds a value of type i32 and a value of type List, so Cons needs an amount of space equal to the size of an i32 plus the size of a List. To figure out how much memory a List needs, it looks at