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

Documentation misleadingly implies that auto-cross-borrowing still occurs #19302

Closed
bstrie opened this issue Nov 25, 2014 · 1 comment · Fixed by #19304
Closed

Documentation misleadingly implies that auto-cross-borrowing still occurs #19302

bstrie opened this issue Nov 25, 2014 · 1 comment · Fixed by #19304

Comments

@bstrie
Copy link
Contributor

bstrie commented Nov 25, 2014

http://doc.rust-lang.org/guide-pointers.html#best-practices

Use references when you want to use a pointer, but do not want to take ownership. References just borrow ownership, which is more polite if you don't need the ownership. In other words, prefer:

fn succ(x: &int) -> int { *x + 1 }

to

fn succ(x: Box<int>) -> int { *x + 1 }

As a corollary to that rule, references allow you to accept a wide variety of other pointers, and so are useful so that you don't have to write a number of variants per pointer. In other words, prefer:

fn succ(x: &int) -> int { *x + 1 }

to

fn box_succ(x: Box<int>) -> int { *x + 1 }

fn rc_succ(x: std::rc::Rc<int>) -> int { *x + 1 }

In the past, a function accepting &int could seamlessly accept an input of type Box<int> (or ~int, rather), what we (I think) we used to call auto-cross-borrowing. But in today's rust you must deref-and-borrow at the call site manually via &*. Mention this last fact as a part of this doc chunk.

@bstrie bstrie added the A-docs label Nov 25, 2014
@bstrie
Copy link
Contributor Author

bstrie commented Nov 25, 2014

cc @steveklabnik

steveklabnik added a commit to steveklabnik/rust that referenced this issue Nov 25, 2014
alexcrichton added a commit to alexcrichton/rust that referenced this issue Nov 27, 2014
Fixes rust-lang#19302.

Also made a minor cosmetic change to bring the example in line with style guidelines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant