Skip to content

Document fully-qualified syntax in as' keyword doc #142670

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 22 additions & 5 deletions library/std/src/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/// Cast between types, or rename an import.
///
/// `as` is most commonly used to turn primitive types into other primitive types, but it has other
/// uses that include turning pointers into addresses, addresses into pointers, and pointers into
/// other pointers.
/// uses that include turning pointers into addresses, addresses into pointers, pointers into
/// other pointers, and qualifying paths for associated items.
///
/// ```rust
/// let thing1: u8 = 89.0 as u8;
Expand Down Expand Up @@ -37,9 +37,26 @@
/// use std::{mem as memory, net as network};
/// // Now you can use the names `memory` and `network` to refer to `std::mem` and `std::net`.
/// ```
/// For more information on what `as` is capable of, see the [Reference].
///
/// [Reference]: ../reference/expressions/operator-expr.html#type-cast-expressions
/// You'll also find with `From` and `Into`, and indeed all traits, that `as` is used for the
/// _fully qualified path_, a means of disambiguating associated items, i.e. functions,
/// constants, and types. For example, if you have a type which implements two traits with identical
/// method names (e.g. `Into::<u32>::into` and `Into::<u64>::into`), you can clarify which method
/// you'll use with `<MyThing as Into<u32>>::into(my_thing)`[^as-use-from]. This is quite verbose,
/// but fortunately, Rust's type inference usually saves you from needing this, although it is
/// occasionally necessary, especially with methods that return a generic type like `Into::into` or
/// methods that don't take `self`. It's more common to use in macros where it can provide necessary
/// hygiene.
///
/// [^as-use-from]: You should probably never use this syntax with `Into` and instead write
/// `T::from(my_thing)`. It just happens that there aren't any great examples for this syntax in
/// the standard library. Also, at time of writing, the compiler tends to suggest fully-qualified
/// paths to fix ambiguous `Into::into` calls, so the example should hopefully be familiar.
///
/// For more information on what `as` is capable of, see the Reference on [type cast expressions]
/// and [qualified paths].
///
/// [type cast expressions]: ../reference/expressions/operator-expr.html#type-cast-expressions
/// [qualified paths]: ../reference/paths.html#qualified-paths
/// [`crate`]: keyword.crate.html
/// [`use`]: keyword.use.html
/// [const-cast]: pointer::cast
Expand Down
Loading