Skip to content

revised pointer example #22473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/doc/trpl/pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,16 @@ duration a *lifetime*. Let's try a more complex example:

```{rust}
fn main() {
let x = &mut 5;
let mut x = 5;

if *x < 10 {
if x < 10 {
let y = &x;

println!("Oh no: {}", y);
return;
}

*x -= 1;
x -= 1;

println!("Oh no: {}", x);
}
Expand All @@ -382,17 +382,18 @@ mutated, and therefore, lets us pass. This wouldn't work:

```{rust,ignore}
fn main() {
let x = &mut 5;
let mut x = 5;

if *x < 10 {
if x < 10 {
let y = &x;
*x -= 1;

x -= 1;

println!("Oh no: {}", y);
return;
}

*x -= 1;
x -= 1;

println!("Oh no: {}", x);
}
Expand All @@ -401,12 +402,12 @@ fn main() {
It gives this error:

```text
test.rs:5:8: 5:10 error: cannot assign to `*x` because it is borrowed
test.rs:5 *x -= 1;
^~
test.rs:4:16: 4:18 note: borrow of `*x` occurs here
test.rs:4 let y = &x;
^~
test.rs:7:9: 7:15 error: cannot assign to `x` because it is borrowed
test.rs:7 x -= 1;
^~~~~~
test.rs:5:18: 5:19 note: borrow of `x` occurs here
test.rs:5 let y = &x;
^
```

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