Skip to content

Commit 5e858f3

Browse files
authoredJul 3, 2016
Auto merge of #34532 - jonmarkprice:master, r=steveklabnik
Book: Small grammatical and stylistic edits to book I've been reading [the book](https://doc.rust-lang.org/book/) and noticed a few small grammatical and stylistic issues which I've rolled into this pull request. I'm not sure if I should do so many small, unrelated edits in a single pull request but it seems like a lot of overhead for each small edit. Maybe one commit per edit but one pull request per file/section? Feedback is very much appreciated as this is my first pull request ever! r? @steveklabnik rollup
2 parents 20183f4 + ec66b5a commit 5e858f3

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed
 

‎src/doc/book/lifetimes.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ lifetime parameters using three easily memorizable and unambiguous rules. This m
290290
acts as a shorthand for writing an item signature, while not hiding
291291
away the actual types involved as full local inference would if applied to it.
292292

293-
When talking about lifetime elision, we use the term *input lifetime* and
293+
When talking about lifetime elision, we use the terms *input lifetime* and
294294
*output lifetime*. An *input lifetime* is a lifetime associated with a parameter
295295
of a function, and an *output lifetime* is a lifetime associated with the return
296296
value of a function. For example, this function has an input lifetime:
@@ -335,11 +335,13 @@ fn print<'a>(s: &'a str); // expanded
335335
336336
fn debug(lvl: u32, s: &str); // elided
337337
fn debug<'a>(lvl: u32, s: &'a str); // expanded
338+
```
338339

339-
// In the preceding example, `lvl` doesn’t need a lifetime because it’s not a
340-
// reference (`&`). Only things relating to references (such as a `struct`
341-
// which contains a reference) need lifetimes.
340+
In the preceding example, `lvl` doesn’t need a lifetime because it’s not a
341+
reference (`&`). Only things relating to references (such as a `struct`
342+
which contains a reference) need lifetimes.
342343

344+
```rust,ignore
343345
fn substr(s: &str, until: u32) -> &str; // elided
344346
fn substr<'a>(s: &'a str, until: u32) -> &'a str; // expanded
345347

‎src/doc/book/ownership.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ But, unlike a move, we can still use `v` afterward. This is because an `i32`
214214
has no pointers to data somewhere else, copying it is a full copy.
215215

216216
All primitive types implement the `Copy` trait and their ownership is
217-
therefore not moved like one would assume, following the ´ownership rules´.
217+
therefore not moved like one would assume, following the ownership rules.
218218
To give an example, the two following snippets of code only compile because the
219219
`i32` and `bool` types implement the `Copy` trait.
220220

@@ -290,6 +290,6 @@ let (v1, v2, answer) = foo(v1, v2);
290290
Ugh! The return type, return line, and calling the function gets way more
291291
complicated.
292292

293-
Luckily, Rust offers a feature, borrowing, which helps us solve this problem.
294-
It’s the topic of the next section!
293+
Luckily, Rust offers a feature which helps us solve this problem.
294+
It’s called borrowing and is the topic of the next section!
295295

‎src/doc/book/references-and-borrowing.md

+24-20
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ let v = vec![];
123123
foo(&v);
124124
```
125125

126-
errors with:
126+
will give us this error:
127127

128128
```text
129129
error: cannot borrow immutable borrowed content `*v` as mutable
@@ -152,8 +152,8 @@ the thing `y` points at. You’ll notice that `x` had to be marked `mut` as well
152152
If it wasn’t, we couldn’t take a mutable borrow to an immutable value.
153153

154154
You'll also notice we added an asterisk (`*`) in front of `y`, making it `*y`,
155-
this is because `y` is a `&mut` reference. You'll also need to use them for
156-
accessing the contents of a reference as well.
155+
this is because `y` is a `&mut` reference. You'll need to use astrisks to
156+
access the contents of a reference as well.
157157

158158
Otherwise, `&mut` references are like references. There _is_ a large
159159
difference between the two, and how they interact, though. You can tell
@@ -179,7 +179,7 @@ As it turns out, there are rules.
179179

180180
# The Rules
181181

182-
Here’s the rules about borrowing in Rust:
182+
Here are the rules for borrowing in Rust:
183183

184184
First, any borrow must last for a scope no greater than that of the owner.
185185
Second, you may have one or the other of these two kinds of borrows, but not
@@ -208,12 +208,14 @@ With this in mind, let’s consider our example again.
208208
Here’s the code:
209209

210210
```rust,ignore
211-
let mut x = 5;
212-
let y = &mut x;
211+
fn main() {
212+
let mut x = 5;
213+
let y = &mut x;
213214
214-
*y += 1;
215+
*y += 1;
215216
216-
println!("{}", x);
217+
println!("{}", x);
218+
}
217219
```
218220

219221
This code gives us this error:
@@ -225,7 +227,7 @@ error: cannot borrow `x` as immutable because it is also borrowed as mutable
225227
```
226228

227229
This is because we’ve violated the rules: we have a `&mut T` pointing to `x`,
228-
and so we aren’t allowed to create any `&T`s. One or the other. The note
230+
and so we aren’t allowed to create any `&T`s. It's one or the other. The note
229231
hints at how to think about this problem:
230232

231233
```text
@@ -243,14 +245,16 @@ In Rust, borrowing is tied to the scope that the borrow is valid for. And our
243245
scopes look like this:
244246

245247
```rust,ignore
246-
let mut x = 5;
247-
248-
let y = &mut x; // -+ &mut borrow of x starts here
249-
// |
250-
*y += 1; // |
251-
// |
252-
println!("{}", x); // -+ - try to borrow x here
253-
// -+ &mut borrow of x ends here
248+
fn main() {
249+
let mut x = 5;
250+
251+
let y = &mut x; // -+ &mut borrow of x starts here
252+
// |
253+
*y += 1; // |
254+
// |
255+
println!("{}", x); // -+ - try to borrow x here
256+
} // -+ &mut borrow of x ends here
257+
254258
```
255259

256260
The scopes conflict: we can’t make an `&x` while `y` is in scope.
@@ -269,12 +273,12 @@ println!("{}", x); // <- try to borrow x here
269273
```
270274

271275
There’s no problem. Our mutable borrow goes out of scope before we create an
272-
immutable one. But scope is the key to seeing how long a borrow lasts for.
276+
immutable one. So scope is the key to seeing how long a borrow lasts for.
273277

274278
## Issues borrowing prevents
275279

276280
Why have these restrictive rules? Well, as we noted, these rules prevent data
277-
races. What kinds of issues do data races cause? Here’s a few.
281+
races. What kinds of issues do data races cause? Here are a few.
278282

279283
### Iterator invalidation
280284

@@ -323,7 +327,7 @@ for i in &v {
323327

324328
We can’t modify `v` because it’s borrowed by the loop.
325329

326-
### use after free
330+
### Use after free
327331

328332
References must not live longer than the resource they refer to. Rust will
329333
check the scopes of your references to ensure that this is true.

0 commit comments

Comments
 (0)
Please sign in to comment.