many small grammatical and stylistic changes

grammatical: "Here's" should be "Here are", "rules" is plural.
stylistic: "rules for" is more idiomatic than "rules about".
grammatical: No verb in "One or the other"; changed to "It's one or the other".
code: added implied `fn main() { ... }` because it is referenced in "note: previous borrow ends here"
semantic: "But" seems like the wrong word here, there is now, contrast, only further explanation. "so", "thus" or "therefor" is clearer.
grammatical: Another misuse of "Here's", should be "Here are" (or possibly "Here're").
grammatical: "use" should be capitalized. All other subheadings capitalize the first word.
This commit is contained in:
Jonathan Price 2016-06-27 18:06:40 -05:00 committed by GitHub
parent 9dd5540540
commit 15a0299299

View File

@ -179,7 +179,7 @@ As it turns out, there are rules.
# The Rules
Heres the rules about borrowing in Rust:
Here are the rules for borrowing in Rust:
First, any borrow must last for a scope no greater than that of the owner.
Second, you may have one or the other of these two kinds of borrows, but not
@ -208,12 +208,14 @@ With this in mind, lets consider our example again.
Heres the code:
```rust,ignore
let mut x = 5;
let y = &mut x;
fn main() {
let mut x = 5;
let y = &mut x;
*y += 1;
*y += 1;
println!("{}", x);
println!("{}", x);
}
```
This code gives us this error:
@ -225,7 +227,7 @@ error: cannot borrow `x` as immutable because it is also borrowed as mutable
```
This is because weve violated the rules: we have a `&mut T` pointing to `x`,
and so we arent allowed to create any `&T`s. One or the other. The note
and so we arent allowed to create any `&T`s. It's one or the other. The note
hints at how to think about this problem:
```text
@ -243,14 +245,16 @@ In Rust, borrowing is tied to the scope that the borrow is valid for. And our
scopes look like this:
```rust,ignore
let mut x = 5;
fn main() {
let mut x = 5;
let y = &mut x; // -+ &mut borrow of x starts here
// |
*y += 1; // |
// |
println!("{}", x); // -+ - try to borrow x here
// -+ &mut borrow of x ends here
let y = &mut x; // -+ &mut borrow of x starts here
// |
*y += 1; // |
// |
println!("{}", x); // -+ - try to borrow x here
} // -+ &mut borrow of x ends here
```
The scopes conflict: we cant make an `&x` while `y` is in scope.
@ -269,12 +273,12 @@ println!("{}", x); // <- try to borrow x here
```
Theres no problem. Our mutable borrow goes out of scope before we create an
immutable one. But scope is the key to seeing how long a borrow lasts for.
immutable one. So scope is the key to seeing how long a borrow lasts for.
## Issues borrowing prevents
Why have these restrictive rules? Well, as we noted, these rules prevent data
races. What kinds of issues do data races cause? Heres a few.
races. What kinds of issues do data races cause? Here are a few.
### Iterator invalidation
@ -323,7 +327,7 @@ for i in &v {
We cant modify `v` because its borrowed by the loop.
### use after free
### Use after free
References must not live longer than the resource they refer to. Rust will
check the scopes of your references to ensure that this is true.