@@ -123,7 +123,7 @@ let v = vec![];
123
123
foo(&v);
124
124
```
125
125
126
- errors with :
126
+ will give us this error :
127
127
128
128
``` text
129
129
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
152
152
If it wasn’t, we couldn’t take a mutable borrow to an immutable value.
153
153
154
154
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.
157
157
158
158
Otherwise, ` &mut ` references are like references. There _ is_ a large
159
159
difference between the two, and how they interact, though. You can tell
@@ -179,7 +179,7 @@ As it turns out, there are rules.
179
179
180
180
# The Rules
181
181
182
- Here’s the rules about borrowing in Rust:
182
+ Here are the rules for borrowing in Rust:
183
183
184
184
First, any borrow must last for a scope no greater than that of the owner.
185
185
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.
208
208
Here’s the code:
209
209
210
210
``` 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;
213
214
214
- *y += 1;
215
+ *y += 1;
215
216
216
- println!("{}", x);
217
+ println!("{}", x);
218
+ }
217
219
```
218
220
219
221
This code gives us this error:
@@ -225,7 +227,7 @@ error: cannot borrow `x` as immutable because it is also borrowed as mutable
225
227
```
226
228
227
229
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
229
231
hints at how to think about this problem:
230
232
231
233
``` text
@@ -243,14 +245,16 @@ In Rust, borrowing is tied to the scope that the borrow is valid for. And our
243
245
scopes look like this:
244
246
245
247
``` 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
+
254
258
```
255
259
256
260
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
269
273
```
270
274
271
275
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.
273
277
274
278
## Issues borrowing prevents
275
279
276
280
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.
278
282
279
283
### Iterator invalidation
280
284
@@ -323,7 +327,7 @@ for i in &v {
323
327
324
328
We can’t modify ` v ` because it’s borrowed by the loop.
325
329
326
- ### use after free
330
+ ### Use after free
327
331
328
332
References must not live longer than the resource they refer to. Rust will
329
333
check the scopes of your references to ensure that this is true.
0 commit comments