diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index ece5d59dce..718cd597a3 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -11,12 +11,13 @@ lines of text in a file or the prices of items in a shopping cart. To create a new empty vector, we call the `Vec::new` function, as shown in Listing 8-1. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-01/src/main.rs:here}} ``` -Listing 8-1: Creating a new, empty vector to hold values -of type `i32` + Note that we added a type annotation here. Because we aren’t inserting any values into this vector, Rust doesn’t know what kind of elements we intend to @@ -35,12 +36,13 @@ new vector that holds the values you give it. Listing 8-2 creates a new because that’s the default integer type, as we discussed in the [“Data Types”][data-types] section of Chapter 3. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-02/src/main.rs:here}} ``` -Listing 8-2: Creating a new vector containing -values + Because we’ve given initial `i32` values, Rust can infer that the type of `v` is `Vec`, and the type annotation isn’t necessary. Next, we’ll look at how @@ -51,12 +53,13 @@ to modify a vector. To create a vector and then add elements to it, we can use the `push` method, as shown in Listing 8-3. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-03/src/main.rs:here}} ``` -Listing 8-3: Using the `push` method to add values to a -vector + As with any variable, if we want to be able to change its value, we need to make it mutable using the `mut` keyword, as discussed in Chapter 3. The numbers @@ -72,12 +75,13 @@ the values that are returned from these functions for extra clarity. Listing 8-4 shows both methods of accessing a value in a vector, with indexing syntax and the `get` method. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-04/src/main.rs:here}} ``` -Listing 8-4: Using indexing syntax and using the `get` -method to access an item in a vector + Note a few details here. We use the index value of `2` to get the third element because vectors are indexed by number, starting at zero. Using `&` and `[]` @@ -91,12 +95,13 @@ existing elements. As an example, let’s see what happens when we have a vector of five elements and then we try to access an element at index 100 with each technique, as shown in Listing 8-5. ++ ```rust,should_panic,panics {{#rustdoc_include ../listings/ch08-common-collections/listing-08-05/src/main.rs:here}} ``` -Listing 8-5: Attempting to access the element at index -100 in a vector containing five elements + When we run this code, the first `[]` method will cause the program to panic because it references a nonexistent element. This method is best used when you @@ -123,12 +128,13 @@ to the first element in a vector and try to add an element to the end. This program won’t work if we also try to refer to that element later in the function. ++ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch08-common-collections/listing-08-06/src/main.rs:here}} ``` -Listing 8-6: Attempting to add an element to a vector -while holding a reference to an item + Compiling this code will result in this error: @@ -156,23 +162,25 @@ elements rather than use indices to access one at a time. Listing 8-7 shows how to use a `for` loop to get immutable references to each element in a vector of `i32` values and print them. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-07/src/main.rs:here}} ``` -Listing 8-7: Printing each element in a vector by -iterating over the elements using a `for` loop + We can also iterate over mutable references to each element in a mutable vector in order to make changes to all the elements. The `for` loop in Listing 8-8 will add `50` to each element. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-08/src/main.rs:here}} ``` -Listing 8-8: Iterating over mutable references to -elements in a vector + To change the value that the mutable reference refers to, we have to use the `*` dereference operator to get to the value in `i` before we can use the `+=` @@ -202,12 +210,13 @@ value types, and all the enum variants will be considered the same type: that of the enum. Then we can create a vector to hold that enum and so, ultimately, hold different types. We’ve demonstrated this in Listing 8-9. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-09/src/main.rs:here}} ``` -Listing 8-9: Defining an `enum` to store values of -different types in one vector + Rust needs to know what types will be in the vector at compile time so it knows exactly how much memory on the heap will be needed to store each element. We @@ -231,12 +240,13 @@ addition to `push`, a `pop` method removes and returns the last element. Like any other `struct`, a vector is freed when it goes out of scope, as annotated in Listing 8-10. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-10/src/main.rs:here}} ``` -Listing 8-10: Showing where the vector and its elements -are dropped + When the vector gets dropped, all of its contents are also dropped, meaning the integers it holds will be cleaned up. The borrow checker ensures that any diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index be0c874748..9494fd94cb 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -41,11 +41,13 @@ of bytes with some extra guarantees, restrictions, and capabilities. An example of a function that works the same way with `Vec` and `String` is the `new` function to create an instance, shown in Listing 8-11. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-11/src/main.rs:here}} ``` -Listing 8-11: Creating a new, empty `String` + This line creates a new, empty string called `s`, into which we can then load data. Often, we’ll have some initial data with which we want to start the @@ -53,12 +55,13 @@ string. For that, we use the `to_string` method, which is available on any type that implements the `Display` trait, as string literals do. Listing 8-12 shows two examples. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-12/src/main.rs:here}} ``` -Listing 8-12: Using the `to_string` method to create a -`String` from a string literal + This code creates a string containing `initial contents`. @@ -66,12 +69,13 @@ We can also use the function `String::from` to create a `String` from a string literal. The code in Listing 8-13 is equivalent to the code in Listing 8-12 that uses `to_string`. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-13/src/main.rs:here}} ``` -Listing 8-13: Using the `String::from` function to create -a `String` from a string literal + Because strings are used for so many things, we can use many different generic APIs for strings, providing us with a lot of options. Some of them can seem @@ -82,12 +86,13 @@ readability. Remember that strings are UTF-8 encoded, so we can include any properly encoded data in them, as shown in Listing 8-14. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-14/src/main.rs:here}} ``` -Listing 8-14: Storing greetings in different languages in -strings + All of these are valid `String` values. @@ -102,24 +107,26 @@ use the `+` operator or the `format!` macro to concatenate `String` values. We can grow a `String` by using the `push_str` method to append a string slice, as shown in Listing 8-15. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-15/src/main.rs:here}} ``` -Listing 8-15: Appending a string slice to a `String` -using the `push_str` method + After these two lines, `s` will contain `foobar`. The `push_str` method takes a string slice because we don’t necessarily want to take ownership of the parameter. For example, in the code in Listing 8-16, we want to be able to use `s2` after appending its contents to `s1`. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-16/src/main.rs:here}} ``` -Listing 8-16: Using a string slice after appending its -contents to a `String` + If the `push_str` method took ownership of `s2`, we wouldn’t be able to print its value on the last line. However, this code works as we’d expect! @@ -128,12 +135,13 @@ The `push` method takes a single character as a parameter and adds it to the `String`. Listing 8-17 adds the letter *l* to a `String` using the `push` method. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-17/src/main.rs:here}} ``` -Listing 8-17: Adding one character to a `String` value -using `push` + As a result, `s` will contain `lol`. @@ -142,12 +150,13 @@ As a result, `s` will contain `lol`. Often, you’ll want to combine two existing strings. One way to do so is to use the `+` operator, as shown in Listing 8-18. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-18/src/main.rs:here}} ``` -Listing 8-18: Using the `+` operator to combine two -`String` values into a new `String` value + The string `s3` will contain `Hello, world!`. The reason `s1` is no longer valid after the addition, and the reason we used a reference to `s2`, has to do @@ -215,12 +224,13 @@ string by referencing them by index is a valid and common operation. However, if you try to access parts of a `String` using indexing syntax in Rust, you’ll get an error. Consider the invalid code in Listing 8-19. ++ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch08-common-collections/listing-08-19/src/main.rs:here}} ``` -Listing 8-19: Attempting to use indexing syntax with a -String + This code will result in the following error: diff --git a/src/ch08-03-hash-maps.md b/src/ch08-03-hash-maps.md index 72331e5b91..057501415a 100644 --- a/src/ch08-03-hash-maps.md +++ b/src/ch08-03-hash-maps.md @@ -24,12 +24,13 @@ One way to create an empty hash map is to use `new` and to add elements with names are *Blue* and *Yellow*. The Blue team starts with 10 points, and the Yellow team starts with 50. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-20/src/main.rs:here}} ``` -Listing 8-20: Creating a new hash map and inserting some -keys and values + Note that we need to first `use` the `HashMap` from the collections portion of the standard library. Of our three common collections, this one is the least @@ -47,12 +48,13 @@ must have the same type. We can get a value out of the hash map by providing its key to the `get` method, as shown in Listing 8-21. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-21/src/main.rs:here}} ``` -Listing 8-21: Accessing the score for the Blue team -stored in the hash map + Here, `score` will have the value that’s associated with the Blue team, and the result will be `10`. The `get` method returns an `Option<&V>`; if there’s no @@ -81,12 +83,13 @@ For types that implement the `Copy` trait, like `i32`, the values are copied into the hash map. For owned values like `String`, the values will be moved and the hash map will be the owner of those values, as demonstrated in Listing 8-22. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-22/src/main.rs:here}} ``` -Listing 8-22: Showing that keys and values are owned by -the hash map once they’re inserted + We aren’t able to use the variables `field_name` and `field_value` after they’ve been moved into the hash map with the call to `insert`. @@ -120,12 +123,13 @@ Even though the code in Listing 8-23 calls `insert` twice, the hash map will only contain one key–value pair because we’re inserting the value for the Blue team’s key both times. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-23/src/main.rs:here}} ``` -Listing 8-23: Replacing a value stored with a particular -key + This code will print `{"Blue": 25}`. The original value of `10` has been overwritten. @@ -147,12 +151,13 @@ we want to check whether the key for the Yellow team has a value associated with it. If it doesn’t, we want to insert the value `50`, and the same for the Blue team. Using the `entry` API, the code looks like Listing 8-24. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-24/src/main.rs:here}} ``` -Listing 8-24: Using the `entry` method to only insert if -the key does not already have a value + The `or_insert` method on `Entry` is defined to return a mutable reference to the value for the corresponding `Entry` key if that key exists, and if not, it @@ -175,12 +180,13 @@ the words as keys and increment the value to keep track of how many times we’v seen that word. If it’s the first time we’ve seen a word, we’ll first insert the value `0`. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-25/src/main.rs:here}} ``` -Listing 8-25: Counting occurrences of words using a hash -map that stores words and counts + This code will print `{"world": 2, "hello": 1, "wonderful": 1}`. You might see the same key–value pairs printed in a different order: recall from the