Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch8 edits after technical review #450

Merged
merged 15 commits into from
Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ growable
hardcoded
hardcoding
hasher
hashmap
hashers
HashMap
Hashmaps
Haskell
hasn
helloworld
Expand Down
376 changes: 167 additions & 209 deletions nostarch/chapter08.md

Large diffs are not rendered by default.

Binary file added nostarch/odt/chapter08.docx
Binary file not shown.
12 changes: 6 additions & 6 deletions src/ch08-00-fundamental-collections.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# Fundamental Collections

Rust's standard library includes a number of really useful data structures
Rusts standard library includes a number of really useful data structures
called *collections*. Most other data types represent one specific value, but
collections can contain multiple values. Unlike the built-in array and tuple
types, the data these collections point to is stored on the heap, which means
the amount of data does not need to be known at compile time and can grow or
shrink as the program runs. Each kind of collection has different capabilities
and costs, and choosing an appropriate one for the situation you're in is a
skill you'll develop over time. In this chapter, we'll go over three
and costs, and choosing an appropriate one for the situation youre in is a
skill youll develop over time. In this chapter, well go over three
collections which are used very often in Rust programs:

* A *vector* allows us to store a variable number of values next to each other.
* A *string* is a collection of characters. We've seen the `String` type
before, but we'll talk about it in depth now.
* A *string* is a collection of characters. Weve seen the `String` type
before, but well talk about it in depth now.
* A *hash map* allows us to associate a value with a particular key.

There are more specialized variants of each of these data structures for
particular situations, but these are the most fundamental and common. We're
particular situations, but these are the most fundamental and common. Were
going to discuss how to create and update each of the collections, as well as
what makes each special.
92 changes: 37 additions & 55 deletions src/ch08-01-vectors.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Vectors

The first type we'll look at is `Vec<T>`, also known as a *vector*. Vectors
The first type well look at is `Vec<T>`, also known as a *vector*. Vectors
allow us to store more than one value in a single data structure that puts all
the values next to each other in memory. Vectors can only store values of the
same type. They are useful in situations where you have a list of items, such
Expand All @@ -14,18 +14,18 @@ To create a new, empty vector, we can call the `Vec::new` function:
let v: Vec<i32> = Vec::new();
```

Note that we added a type annotation here. Since we aren't inserting any values
into this vector, Rust doesn't know what kind of elements we intend to store.
This is an important point. Vectors are homogeneous: they may store many values,
but those values must all be the same type. Vectors are implemented using
generics, which Chapter 10 will cover how to use in your own types. For now,
all you need to know is that the `Vec` type provided by the standard library
can hold any type, and when a specific `Vec` holds a specific type, the type
goes within angle brackets. We've told Rust that the `Vec` in `v` will hold
elements of the `i32` type.
Note that we added a type annotation here. Since we arent inserting any values
into this vector, Rust doesnt know what kind of elements we intend to store.
This is an important point. Vectors are homogeneous: they may store many
values, but those values must all be the same type. Vectors are implemented
using generics, which Chapter 10 will cover how to use in your own types. For
now, all you need to know is that the `Vec` type provided by the standard
library can hold any type, and when a specific `Vec` holds a specific type, the
type goes within angle brackets. Weve told Rust that the `Vec` in `v` will
hold elements of the `i32` type.

In real code, Rust can infer the type of value we want to store once we insert
values, so you rarely need to do this type annotation. It's more common to
values, so you rarely need to do this type annotation. Its more common to
create a `Vec` that has initial values, and Rust provides the `vec!` macro for
convenience. The macro will create a new `Vec` that holds the values we give
it. This will create a new `Vec<i32>` that holds the values `1`, `2`, and `3`:
Expand All @@ -34,8 +34,8 @@ it. This will create a new `Vec<i32>` that holds the values `1`, `2`, and `3`:
let v = vec![1, 2, 3];
```

Because we've given initial `i32` values, Rust can infer that the type of `v`
is `Vec<i32>`, and the type annotation isn't necessary. Let's look at how to
Because weve given initial `i32` values, Rust can infer that the type of `v`
is `Vec<i32>`, and the type annotation isnt necessary. Lets look at how to
modify a vector next.

### Updating a Vector
Expand All @@ -54,7 +54,7 @@ v.push(8);
As with any variable as we discussed in Chapter 3, if we want to be able to
change its value, we need to make it mutable with the `mut` keyword. The
numbers we place inside are all `i32`s, and Rust infers this from the data, so
we don't need the `Vec<i32>` annotation.
we dont need the `Vec<i32>` annotation.

### Dropping a Vector Drops its Elements

Expand All @@ -72,13 +72,13 @@ Like any other `struct`, a vector will be freed when it goes out of scope:
When the vector gets dropped, all of its contents will also be dropped, meaning
those integers it holds will be cleaned up. This may seem like a
straightforward point, but can get a little more complicated once we start to
introduce references to the elements of the vector. Let's tackle that next!
introduce references to the elements of the vector. Lets tackle that next!

### Reading Elements of Vectors

Now that you know how to create, update, and destroy vectors, knowing how to
read their contents is a good next step. There are two ways to reference a
value stored in a vector. In the examples, we've annotated the types of the
value stored in a vector. In the examples, weve annotated the types of the
values that are returned from these functions for extra clarity.

This example shows both methods of accessing a value in a vector either with
Expand All @@ -94,12 +94,12 @@ let third: Option<&i32> = v.get(2);
There are a few things to note here. First, that we use the index value of `2`
to get the third element: vectors are indexed by number, starting at zero.
Second, the two different ways to get the third element are: using `&` and
`[]`s, which gives us a reference, or using the `get` method with the index
`[]`, which gives us a reference, or using the `get` method with the index
passed as an argument, which gives us an `Option<&T>`.

The reason Rust has two ways to reference an element is so that you can choose
how the program behaves when you try to use an index value that the vector
doesn't have an element for. As an example, what should a program do if it has
doesnt have an element for. As an example, what should a program do if it has
a vector that holds five elements then tries to access an element at index 100
like this:

Expand All @@ -117,12 +117,12 @@ element past the end of the vector to be a fatal error that should crash the
program.

When the `get` method is passed an index that is outside the array, it will
return `None` without `panic!`ing. You would use this if accessing an element
beyond the range of the vector will happen occasionally under normal
return `None` without causing a `panic!` call. You would use this if accessing
an element beyond the range of the vector will happen occasionally under normal
circumstances. Your code can then have logic to handle having either
`Some(&element)` or `None`, as we discussed in Chapter 6. For example, the
index could be coming from a person entering a number. If they accidentally
enter a number that's too large and your program gets a `None` value, you could
enter a number thats too large and your program gets a `None` value, you could
tell the user how many items are in the current `Vec` and give them another
chance to enter a valid value. That would be more user-friendly than crashing
the program for a typo!
Expand All @@ -132,7 +132,7 @@ the program for a typo!
Once the program has a valid reference, the borrow checker will enforce the
ownership and borrowing rules covered in Chapter 4 to ensure this reference and
any other references to the contents of the vector stay valid. Recall the rule
that says we can't have mutable and immutable references in the same scope.
that says we cant have mutable and immutable references in the same scope.
That rule applies in this example, where we hold an immutable reference to the
first element in a vector and try to add an element to the end:

Expand All @@ -147,7 +147,8 @@ v.push(6);
Compiling this will give us this error:

```text
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as
immutable
|
4 | let first = &v[0];
| - immutable borrow occurs here
Expand All @@ -160,26 +161,26 @@ error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immuta

This code might look like it should work: why should a reference to the first
element care about what changes about the end of the vector? The reason why
this code isn't allowed is due to the way vectors work. Adding a new element
this code isnt allowed is due to the way vectors work. Adding a new element
onto the end of the vector might require allocating new memory and copying the
old elements over to the new space, in the circumstance that there isn't enough
old elements over to the new space, in the circumstance that there isnt enough
room to put all the elements next to each other where the vector was. In that
case, the reference to the first element would be pointing to deallocated
memory. The borrowing rules prevent programs from ending up in that situation.

> Note: For more on this, see [The Nomicon][nomicon].

[nomicon]: https://doc.rust-lang.org/stable/nomicon/vec.html
> Note: For more on this, see The Nomicon at
*https://doc.rust-lang.org/stable/nomicon/vec.html*.

### Using an Enum to Store Multiple Types

At the beginning of this chapter, we said that vectors can only store values
that are all the same type. This can be inconvenient; there are definitely use
cases for needing to store a list of things of different types. Luckily, the
variants of an enum are all defined under the same enum type, so when we need to
store elements of a different type in a vector, we can define and use an enum!
variants of an enum are all defined under the same enum type, so when we need
to store elements of a different type in a vector, we can define and use an
enum!

For example, let's say we want to get values from a row in a spreadsheet, where
For example, lets say we want to get values from a row in a spreadsheet, where
some of the columns in the row contain integers, some floating point numbers,
and some strings. We can define an enum whose variants will hold the different
value types, and then all of the enum variants will be considered the same
Expand Down Expand Up @@ -209,32 +210,13 @@ cause errors with the operations performed on the elements of the vector. Using
an enum plus a `match` means that Rust will ensure at compile time that we
always handle every possible case, as we discussed in Chapter 6.

<!-- Can you briefly explain what the match is doing here, as a recap? How does
it mean we always handle every possible case? I'm not sure it's totally clear.
-->
<!-- Because this is a focus of chapter 6 rather than this chapter's focus, we
don't think we should repeat it here as well, but we added a reference. /Carol
-->

If you don't know at the time that you're writing a program the exhaustive set
If you don’t know at the time that you’re writing a program the exhaustive set
of types the program will get at runtime to store in a vector, the enum
technique won't work. Instead, you can use a trait object, which we'll cover in
Chapter 13.
technique wont work. Instead, you can use a trait object, which well cover in
Chapter 17.

Now that we've gone over some of the most common ways to use vectors, be sure
Now that weve gone over some of the most common ways to use vectors, be sure
to take a look at the API documentation for all of the many useful methods
defined on `Vec` by the standard library. For example, in addition to `push`
there's a `pop` method that will remove and return the last element. Let's move
theres a `pop` method that will remove and return the last element. Lets move
on to the next collection type: `String`!

<!-- Do you mean the Rust online documentation here? Are you not including it
in the book for space reasons? We might want to justify sending them out of the
book if we don't want to cover it here -->

<!-- Yes, there are many, many methods on Vec: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html
Also there are occasionally new methods available with new versions of the
language, so there's no way we can be comprehensive here. We want the reader to
use the API documentation in these situations since the purpose of the online
docs is to be comprehensive and up to date. I personally wouldn't expect a book
like this to duplicate the info that's in the API docs, so I don't think a
justification is necessary here. /Carol -->
Loading