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

Improve doc for 'as _' #78086

Merged
merged 1 commit into from
Nov 28, 2020
Merged

Improve doc for 'as _' #78086

merged 1 commit into from
Nov 28, 2020

Conversation

poliorcetics
Copy link
Contributor

Fix #78042.

@rustbot modify labels: A-coercions T-doc

@rustbot rustbot added the A-coercions Area: implicit and explicit `expr as Type` coercions label Oct 18, 2020
@rustbot rustbot added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Oct 18, 2020
@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 18, 2020
@Mark-Simulacrum
Copy link
Member

cc @jonas-schievink

I am not sure that this is a good thing to document. I think it can be confusing to see foo as _, but it feels like that's sort of a reason to not do that rather than a reason to document it. Certainly it makes inference breakage even more likely than alternative patterns.

Perhaps we could add a note that "usually, such code should be written with an explicit type"?

@poliorcetics
Copy link
Contributor Author

I added a note about clarity and stability, is it better ?

@cher-nov
Copy link

I would also request for some clarification of similarities and differences between as / From:: and as _ / .into(), as was noted in the original issue.

@poliorcetics
Copy link
Contributor Author

From and Into are not as easily separated, here is a counter example:

fn main() {
    let a = 1_u8;
    let b = a.into(); // The only in the snippet error occurs here.

    let c = From::from(a);
    take(c);
}

fn take(_: u16) {}

@cher-nov
Copy link

From and Into are not as easily separated, here is a counter example:

Well, what I actually meant is to clarify a bit why do we need as T and as _ if we also have From::<T> and .into().
I'm just learning Rust now, and this question wasn't intuitive for me at all. So I would appreciate some notes on this to make newbies experience a bit easier. :)

@poliorcetics
Copy link
Contributor Author

poliorcetics commented Oct 25, 2020

Oh sorry, I didn't understand what you wanted. I'm not sure how to explain it though.

Quick draft below, is it enough ?

`as` can be seen as the primitive for `From` and `Into`: `as` only works 
with primitives (`u8`, `bool`, `str`, pointers, ...) whereas `From` and `Into` 
also works with types like `String` or `Vec`.

I'm not sure if the part about as only working with primitives is completely true.

@cher-nov
Copy link

Oh sorry, I didn't understand what you wanted. I'm not sure how to explain it though.

Oh, nevermind. I'm not a native English speaker, so it's pretty possible that I just can't describe what I want properly.

Quick draft below, is it enough ?

LGTM ;)

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 26, 2020
@poliorcetics
Copy link
Contributor Author

I removed the example and added a point about *const/mut _ specifically, putting a link towards the cast methods for both.

@jyn514 jyn514 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 17, 2020
@Mark-Simulacrum
Copy link
Member

It looks like there's a merge conflict; but the current text looks okay to me. I am still not 100% on this being a good thing to add, but it does seem like some folks at least are in favor so I'm not going to block it, and I could see it being useful.

r=me with conflicts fixed

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 18, 2020
@Dylan-DPC-zz
Copy link

@poliorcetics if you can resolve the conflicts, we can merge this

@poliorcetics
Copy link
Contributor Author

poliorcetics commented Nov 19, 2020

Sorry I completely forgot about this !

Edit: I had to merge my changes to the docs with some other so the text changed a little and probably deserves another read-over

@poliorcetics
Copy link
Contributor Author

@rustbot modify labels: -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 19, 2020
@Mark-Simulacrum
Copy link
Member

Please squash commits; we should not have a "Fix rebase" commit in history if it can be avoided.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 22, 2020
@poliorcetics
Copy link
Contributor Author

@rustbot modify labels: -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 23, 2020
@Mark-Simulacrum
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Nov 28, 2020

📌 Commit e31e627 has been approved by Mark-Simulacrum

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 28, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 28, 2020
…as-schievink

Rollup of 10 pull requests

Successful merges:

 - rust-lang#78086 (Improve doc for 'as _')
 - rust-lang#78853 (rustc_parse: fix ConstBlock expr span)
 - rust-lang#79234 (Resolve typedefs in HashMap gdb/lldb pretty-printers)
 - rust-lang#79344 (Convert UNC path to local path to satisfy install script on Windows)
 - rust-lang#79383 (Fix bold code formatting in keyword docs)
 - rust-lang#79460 (Remove intermediate vectors from `add_bounds`)
 - rust-lang#79474 (Change comments on types to doc-comments)
 - rust-lang#79476 (Sync rustc_codegen_cranelift)
 - rust-lang#79478 (Expand docs on Peekable::peek_mut)
 - rust-lang#79486 (Slightly improve code samples in E0591)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 772b1a6 into rust-lang:master Nov 28, 2020
@rustbot rustbot added this to the 1.50.0 milestone Nov 28, 2020
@poliorcetics poliorcetics deleted the as-placeholder branch November 28, 2020 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document the "as _" (as-underscore) syntax for value coercion / type casting
9 participants