Skip to content
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

rust book: syntax simplications from (autoderef?) #23445

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/doc/trpl/ownership.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ We have to change our `main` a bit too:

```rust
fn main() {
let mut x = 5;
let mut x = 5; // FIXME: remove this line?
// let mut x = Box::new(5); // This (box), used in previous exampl(s), still works

add_one(&mut x);

Expand Down
35 changes: 19 additions & 16 deletions src/doc/trpl/pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ fn box_succ(x: Box<i32>) -> i32 { *x + 1 }
fn rc_succ(x: Rc<i32>) -> i32 { *x + 1 }
```

Note that the caller of your function will have to modify their calls slightly:
Note that the caller of your function will have to modify their calls slightly
(FIXME: actually they don't):

```{rust}
use std::rc::Rc;
Expand All @@ -466,8 +467,8 @@ let box_x = Box::new(5);
let rc_x = Rc::new(5);

succ(ref_x);
succ(&*box_x);
succ(&*rc_x);
succ(&*box_x); // FIXME: actually &box_x works fine
succ(&*rc_x); // FIXME: actually &rc_x works fine
```

The initial `*` dereferences the pointer, and then `&` takes a reference to
Expand Down Expand Up @@ -560,38 +561,40 @@ fn main() {
In this case, Rust knows that `x` is being *borrowed* by the `add_one()`
function, and since it's only reading the value, allows it.

We can borrow `x` multiple times, as long as it's not simultaneous:
We can borrow `x` as read-only multiple times, even simultaneously:

```{rust}
fn add_one(x: &i32) -> i32 {
*x + 1
fn add(x: &i32, y: &i32) -> i32 {
*x + *y
}

fn main() {
let x = Box::new(5);

println!("{}", add_one(&*x));
println!("{}", add_one(&*x));
println!("{}", add_one(&*x));
println!("{}", add(&x, &x));
println!("{}", add(&x, &x));
}
```

Or as long as it's not a mutable borrow. This will error:
We can mutably borrow `x` multiple times, but only if x itself is mutable, and
it may not be *simultaneously* borrowed:

```{rust,ignore}
fn add_one(x: &mut i32) -> i32 {
*x + 1
fn increment(x: &mut i32) {
*x += 1;
}

fn main() {
let x = Box::new(5);
// If variable x is not "mut", this will not compile
let mut x = Box::new(5);

println!("{}", add_one(&*x)); // error: cannot borrow immutable dereference
// of `&`-pointer as mutable
increment(&mut x);
increment(&mut x);
println!("{}", x);
}
```

Notice we changed the signature of `add_one()` to request a mutable reference.
Notice the signature of `increment()` requests a mutable reference.

## Best practices

Expand Down