Snapshot of 5-8 after page review

This commit is contained in:
Carol (Nichols || Goulding)
2017-11-27 15:23:26 -05:00
parent 4293b433db
commit dc0507fa83
4 changed files with 23 additions and 19 deletions

View File

@@ -678,7 +678,7 @@ parameter will be by looking at the code that calls the method:
read `rect2` (rather than write, which would mean wed need a mutable borrow),
and we want `main` to retain ownership of `rect2` so we can use it again after
calling the `can_hold` method. The return value of `can_hold` will be a
boolean, and the implementation will check whether the width and height of
Boolean, and the implementation will check whether the width and height of
`self` are both greater than the width and height of the other `Rectangle`,
respectively. Lets add the new `can_hold` method to the `impl` block from
Listing 5-13, shown in Listing 5-15:

View File

@@ -432,7 +432,7 @@ as its patterns.
Lets 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 seems very similar to an expression used with `if`, but theres a
big difference: with `if`, the expression needs to return a boolean value.
big difference: with `if`, the expression needs to return a Boolean value.
Here, it can be any type. The type of `coin` in this example is the `Coin` enum
that we defined in Listing 6-3.

View File

@@ -231,7 +231,7 @@ These would be good reasons to separate the `client`, `network`, and `server`
modules from *src/lib.rs* and place them into their own files.
First, replace the `client` module code with only the declaration of the
`client` module, so that your *src/lib.rs* looks like the following:
`client` module, so that your *src/lib.rs* looks like code shown in Listing 7-4:
Filename: src/lib.rs
@@ -249,6 +249,9 @@ mod network {
}
```
Listing 7-4: Extracting the contents of the `client` module but leaving the
declaration in *src/lib.rs*
Were still *declaring* the `client` module here, but by replacing the block
with a semicolon, were telling Rust to look in another location for the code
defined within the scope of the `client` module. In other words, the line `mod
@@ -372,7 +375,7 @@ fn connect() {
}
```
When we try to `cargo build`, well get the error shown in Listing 7-4:
When we try to `cargo build`, well get the error shown in Listing 7-5:
```
$ cargo build
@@ -395,14 +398,14 @@ note: ... or maybe `use` the module `server` instead of possibly redeclaring it
| ^^^^^^
```
Listing 7-4: Error when trying to extract the `server` submodule into
Listing 7-5: Error when trying to extract the `server` submodule into
*src/server.rs*
The error says we `cannot declare a new module at this location` and is
pointing to the `mod server;` line in *src/network.rs*. So *src/network.rs* is
different than *src/lib.rs* somehow: keep reading to understand why.
The note in the middle of Listing 7-4 is actually very helpful because it
The note in the middle of Listing 7-5 is actually very helpful because it
points out something we havent yet talked about doing:
```
@@ -509,7 +512,7 @@ Next, well talk about the `pub` keyword and get rid of those warnings!
## Controlling Visibility with `pub`
We resolved the error messages shown in Listing 7-4 by moving the `network` and
We resolved the error messages shown in Listing 7-5 by moving the `network` and
`network::server` code into the *src/network/mod.rs* and
*src/network/server.rs* files, respectively. At that point, `cargo build` was
able to build our project, but we still get warning messages about the
@@ -750,7 +753,7 @@ Overall, these are the rules for item visibility:
### Privacy Examples
Lets look at a few more privacy examples to get some practice. Create a new
library project and enter the code in Listing 7-5 into your new projects
library project and enter the code in Listing 7-6 into your new projects
*src/lib.rs*:
Filename: src/lib.rs
@@ -776,7 +779,7 @@ fn try_me() {
}
```
Listing 7-5: Examples of private and public functions, some of which are
Listing 7-6: Examples of private and public functions, some of which are
incorrect
Before you try to compile this code, make a guess about which lines in the
@@ -826,7 +829,7 @@ Next, lets talk about bringing items into scope with the `use` keyword.
Weve covered how to call functions defined within a module using the module
name as part of the call, as in the call to the `nested_modules` function shown
here in Listing 7-6:
here in Listing 7-7:
Filename: src/main.rs
@@ -844,7 +847,7 @@ fn main() {
}
```
Listing 7-6: Calling a function by fully specifying its enclosing modules path
Listing 7-7: Calling a function by fully specifying its enclosing modules path
As you can see, referring to the fully qualified name can get quite lengthy.
Fortunately, Rust has a keyword to make these calls more concise.
@@ -1081,7 +1084,7 @@ $ cargo test
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
```
## Summary

View File

@@ -194,15 +194,16 @@ to an item
Compiling this code will result in this error:
```
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
4 | let first = &v[0];
| - immutable borrow occurs here
5 |
6 | v.push(6);
| ^ mutable borrow occurs here
7 | }
6 | v.push(6);
| ^ mutable borrow occurs here
7 |
8 | }
| - immutable borrow ends here
```