Skip to content

Commit 8e0e87f

Browse files
author
Junseok Lee
committed
revised pointer example
1 parent dfc5c0f commit 8e0e87f

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/doc/trpl/pointers.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,16 @@ duration a *lifetime*. Let's try a more complex example:
361361

362362
```{rust}
363363
fn main() {
364-
let x = &mut 5;
364+
let mut x = 5;
365365
366-
if *x < 10 {
366+
if x < 10 {
367367
let y = &x;
368368
369369
println!("Oh no: {}", y);
370370
return;
371371
}
372372
373-
*x -= 1;
373+
x -= 1;
374374
375375
println!("Oh no: {}", x);
376376
}
@@ -382,17 +382,18 @@ mutated, and therefore, lets us pass. This wouldn't work:
382382

383383
```{rust,ignore}
384384
fn main() {
385-
let x = &mut 5;
385+
let mut x = 5;
386386
387-
if *x < 10 {
387+
if x < 10 {
388388
let y = &x;
389-
*x -= 1;
389+
390+
x -= 1;
390391
391392
println!("Oh no: {}", y);
392393
return;
393394
}
394395
395-
*x -= 1;
396+
x -= 1;
396397
397398
println!("Oh no: {}", x);
398399
}
@@ -401,12 +402,12 @@ fn main() {
401402
It gives this error:
402403

403404
```text
404-
test.rs:5:8: 5:10 error: cannot assign to `*x` because it is borrowed
405-
test.rs:5 *x -= 1;
406-
^~
407-
test.rs:4:16: 4:18 note: borrow of `*x` occurs here
408-
test.rs:4 let y = &x;
409-
^~
405+
test.rs:7:9: 7:15 error: cannot assign to `x` because it is borrowed
406+
test.rs:7 x -= 1;
407+
^~~~~~
408+
test.rs:5:18: 5:19 note: borrow of `x` occurs here
409+
test.rs:5 let y = &x;
410+
^
410411
```
411412

412413
As you might guess, this kind of analysis is complex for a human, and therefore

0 commit comments

Comments
 (0)