Skip to content

Improve docs differentiating Borrow and AsRef #24163

New issue

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

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

Already on GitHub? # to your account

Closed
pnkfelix opened this issue Apr 7, 2015 · 1 comment · Fixed by #24996
Closed

Improve docs differentiating Borrow and AsRef #24163

pnkfelix opened this issue Apr 7, 2015 · 1 comment · Fixed by #24996

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Apr 7, 2015

Spawned of off #24140

This should be better documented, but there's definitely a reason that things are set up this way.
...
... it may be worth opening a separate issue for improved documentation and missing impls on AsRef.

@pnkfelix pnkfelix added the A-docs label Apr 7, 2015
@steveklabnik
Copy link
Member

Notes from talking to @aturon

borrow is used in things like hashmap, where you have a key, and the map needs to store the owned version, but you want to look things up by the borrowed version. HashMap<String, i32>, but you want to look up by string literals: &str.

for most types, you want T and &T. but for things that take slices, there's a few different slice types. Borrow is good for this

it's more restrictive than asref, because the assumption is the borrow is the same wrt hashing or comparison: String, &str, and &String all hash to the same value.

AsRef: you can get a different reference out of this. that's it. a conversion, not an equivalence.

anything htat impls borrow should implement asref, but no blanket because of coherence

steveklabnik added a commit to steveklabnik/rust that referenced this issue Apr 30, 2015
These two traits are commonly confused. As such, explain the difference.

Fixes rust-lang#24163
steveklabnik added a commit to steveklabnik/rust that referenced this issue May 12, 2015
These two traits are commonly confused. As such, explain the difference.

Fixes rust-lang#24163
steveklabnik added a commit to steveklabnik/rust that referenced this issue May 12, 2015
These two traits are commonly confused. As such, explain the difference.

Fixes rust-lang#24163

r? @aturon
Manishearth added a commit to Manishearth/rust that referenced this issue May 12, 2015
These two traits are commonly confused. As such, explain the difference.

Fixes rust-lang#24163

r? @aturon
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants