mirror of
https://github.com/rust-lang/book.git
synced 2026-09-15 04:20:07 -04:00
Snapshot of 5-8 after page review
This commit is contained in:
@@ -678,7 +678,7 @@ parameter will be by looking at the code that calls the method:
|
||||
read `rect2` (rather than write, which would mean we’d 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. Let’s add the new `can_hold` method to the `impl` block from
|
||||
Listing 5-13, shown in Listing 5-15:
|
||||
|
||||
@@ -432,7 +432,7 @@ as its patterns.
|
||||
Let’s 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 there’s 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.
|
||||
|
||||
|
||||
@@ -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*
|
||||
|
||||
We’re still *declaring* the `client` module here, but by replacing the block
|
||||
with a semicolon, we’re 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`, we’ll get the error shown in Listing 7-4:
|
||||
When we try to `cargo build`, we’ll 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 haven’t yet talked about doing:
|
||||
|
||||
```
|
||||
@@ -509,7 +512,7 @@ Next, we’ll 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
|
||||
|
||||
Let’s 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 project’s
|
||||
library project and enter the code in Listing 7-6 into your new project’s
|
||||
*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, let’s talk about bringing items into scope with the `use` keyword.
|
||||
|
||||
We’ve 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 module’s path
|
||||
Listing 7-7: Calling a function by fully specifying its enclosing module’s 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
|
||||
|
||||
@@ -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
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user