File tree 1 file changed +8
-8
lines changed
1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change @@ -244,8 +244,8 @@ three. The ownership system in Rust does this through a concept called
244
244
Remember the function that borrowed an ` i32 ` ? Let's look at it again.
245
245
246
246
``` rust
247
- fn add_one (num : & i32 ) -> i32 {
248
- * num + 1
247
+ fn add_one (num : & mut i32 ) {
248
+ * num += 1 ;
249
249
}
250
250
```
251
251
@@ -255,8 +255,8 @@ cover the others later. Without eliding the lifetimes, `add_one` looks like
255
255
this:
256
256
257
257
``` rust
258
- fn add_one <'a >(num : & 'a i32 ) -> i32 {
259
- * num + 1
258
+ fn add_one <'a >(num : & 'a mut i32 ) {
259
+ * num += 1 ;
260
260
}
261
261
```
262
262
@@ -278,12 +278,12 @@ fn add_two<'a, 'b>(...)
278
278
Then in our parameter list, we use the lifetimes we've named:
279
279
280
280
``` {rust,ignore}
281
- ...(num: &'a i32) -> ...
281
+ ...(num: &'a mut i32)
282
282
```
283
283
284
- If you compare ` &i32 ` to ` &'a i32 ` , they're the same, it's just that the
285
- lifetime ` 'a ` has snuck in between the ` & ` and the ` i32 ` . We read ` &i32 ` as "a
286
- reference to an i32" and ` &'a i32 ` as "a reference to an i32 with the lifetime 'a.'"
284
+ If you compare ` &mut i32 ` to ` &'a mut i32 ` , they're the same, it's just that the
285
+ lifetime ` 'a ` has snuck in between the ` & ` and the ` mut i32` . We read ` &mut i32 ` as "a
286
+ mutable reference to an i32" and ` &'a mut i32 ` as "a mutable reference to an i32 with the lifetime 'a.'"
287
287
288
288
Why do lifetimes matter? Well, for example, here's some code:
289
289
You can’t perform that action at this time.
0 commit comments