Bump in headings a level

This commit is contained in:
Carol (Nichols || Goulding) 2016-09-26 14:08:25 -04:00
parent 50acc06a9b
commit dcccf74b80
3 changed files with 27 additions and 25 deletions

View File

@ -1,10 +1,10 @@
# Vectors
## Vectors
The first type we'll look at is `Vec<T>`, also known as a *vector*. Vectors
allow you to store more than one value in a single data structure that puts all
the values next to each other in memory.
## Creating a New Vector
### Creating a New Vector
To create a new vector, you can call the `new` function:
@ -24,7 +24,7 @@ That said, in real code, you very rarely need to do this type annotation since
Rust can infer the type of value we want to store once we insert values. Let's
look at how to modify a vector next.
## Updating a Vector
### Updating a Vector
To put elements in the vector, we can use the `push` method:
@ -50,7 +50,7 @@ let v = vec![5, 6, 7, 8];
This macro does a similar thing to our previous example, but it's much more
convenient.
## Dropping a Vector Drops its Elements
### Dropping a Vector Drops its Elements
Like any other `struct`, a vector will be freed when it goes out of scope:
@ -68,7 +68,7 @@ integers are going to be cleaned up as well. 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!
## Reading Elements of Vectors
### Reading Elements of Vectors
Now that we know how creating and destroying vectors works, knowing how to read
their contents is a good next step. There are two ways to reference a value

View File

@ -1,9 +1,9 @@
# Strings
## Strings
We've already talked about strings a bunch in Chapter 4, but let's take a more
in-depth look at them now.
## Many Kinds of Strings
### Many Kinds of Strings
Strings are a common place for new Rustaceans to get stuck. This is due to a
combination of three things: Rust's propensity for making sure to expose
@ -33,7 +33,7 @@ represented in memory in a different way, for example. We won't be talking
about these other string types in this chapter; see their API documentation for
more about how to use them and when each is appropriate.
## Creating a New String
### Creating a New String
Let's look at how to do the same operations on `String` as we did with `Vec`,
starting with creating one. Similarly, `String` has `new`:
@ -85,11 +85,11 @@ let hello = "Здравствуйте";
let hello = "Hola";
```
## Updating a String
### Updating a String
A `String` can be changed and can grow in size, just like a `Vec` can.
### Push
#### Push
You can grow a `String` by using the `push_str` method to append another
string:
@ -119,7 +119,7 @@ s.clear();
Now `s` will be the empty string, "".
### Concatenation
#### Concatenation
Often, you'll want to combine two strings together. One way is to use the `+`
operator:
@ -187,7 +187,7 @@ same way as `println!`, but instead of printing the output to the screen, it
returns a `String` with the contents. This version is much easier to read than
all of the `+`s.
## Indexing into Strings
### Indexing into Strings
In many other languages, accessing individual characters in a string by
referencing the characters by index is a valid and common operation. In Rust,
@ -214,7 +214,7 @@ The error and the note tell the story: Rust strings don't support indexing. So
the follow-up question is, why not? In order to answer that, we have to talk a
bit about how Rust stores strings in memory.
### Internal Representation
#### Internal Representation
A `String` is a wrapper over a `Vec<u8>`. Let's take a look at some of our
properly-encoded UTF-8 example strings from before. First, this one:
@ -250,7 +250,7 @@ should `answer` be `208`? `208` is not a valid character on its own, though.
Plus, for latin letters, this would not return the answer most people would
expect: `&"hello"[0]` would then return `104`, not `h`.
### Bytes and Scalar Values and Grapheme Clusters! Oh my!
#### Bytes and Scalar Values and Grapheme Clusters! Oh my!
This leads to another point about UTF-8: there are really three relevant ways
to look at strings, from Rust's perspective: bytes, scalar values, and grapheme
@ -289,7 +289,7 @@ matter how we define "character".
All of these problems mean that Rust does not implement `[]` for `String`, so
we cannot directly do this.
## Slicing Strings
### Slicing Strings
However, indexing the bytes of a string is very useful, and is not expected to
be fast. While you can't use `[]` with a single number, you _can_ use `[]` with
@ -313,9 +313,11 @@ thread 'main' panicked at 'index 0 and/or 1 in `Здравствуйте` do not
character boundary', ../src/libcore/str/mod.rs:1694
```
## Methods for Iterating Over Strings
### Methods for Iterating Over Strings
If we do need to perform operations on individual characters, the best way to do that is using the `chars` method. Calling `chars` on "नमस्ते" gives us the six Rust `char` values:
If we do need to perform operations on individual characters, the best way to
do that is using the `chars` method. Calling `chars` on "नमस्ते" gives us the six
Rust `char` values:
```rust
for c in "नमस्ते".chars() {

View File

@ -1,4 +1,4 @@
# HashMaps
## HashMaps
The last of our essential collections is the *HashMap*. A `HashMap<K, V>`
stores a mapping of keys of type `K` to values of type `V`. It does this via a
@ -11,7 +11,7 @@ We'll go over the basic API in this chapter, but there are many more goodies
hiding in the functions defined on HashMap by the standard library. As always,
check the standard library documentation for more information.
## Creating a HashMap
### Creating a HashMap
We can create an empty HashMap with `new`, and add elements with `insert`:
@ -76,7 +76,7 @@ the HashMap. The values that the references point to must be valid for at least
as long as the HashMap is valid, though. We will talk more about these issues
in the Lifetimes section of Chapter XX.
## Accessing Values in a HashMap
### Accessing Values in a HashMap
We can get a value out of the HashMap by providing its key to the `get` method:
@ -119,9 +119,9 @@ This will print:
2: world
```
## Updating a HashMap
### Updating a HashMap
### Overwriting a Value
#### Overwriting a Value
If you insert a key and a value, then insert that key with a different value, the value associated with that key will be replaced. Even though this code calls `insert` twice, the HashMap will only contain one key/value pair, since we're inserting with the key `1` both times:
@ -138,7 +138,7 @@ println!("{:?}", map);
This will print `{1: "Hi There"}`.
### Only Insert If the Key Has No Value
#### Only Insert If the Key Has No Value
It's common to want to see if there's some sort of value already stored in the
HashMap for a particular key, and if not, insert a value. HashMaps have a
@ -183,7 +183,7 @@ insert the key `2` with the value "world", since `2` doesn't have a value
already. The second call to `entry` will not change the HashMap since `1`
already has the value "hello".
### Update a Value Based on the Old Value
#### Update a Value Based on the Old Value
Another common use case for HashMaps is to look up a key's value then update
it, using the old value. For instance, if we wanted to count how many times
@ -214,7 +214,7 @@ variable binding, so in order to assign to that value we must first dereference
the end of the `for` loop, so all of these changes are safe and allowed by the
borrowing rules.
## Hashing Function
### Hashing Function
By default, HashMap uses a cryptographically secure hashing function that can
provide resistance to Denial of Service (DoS) attacks. This is not the fastest