Skip to content

Commit c7fd35f

Browse files
committed
Document fully-qualified syntax in as' keyword doc
1 parent 55d4364 commit c7fd35f

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

library/std/src/keyword_docs.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/// Cast between types, or rename an import.
44
///
55
/// `as` is most commonly used to turn primitive types into other primitive types, but it has other
6-
/// uses that include turning pointers into addresses, addresses into pointers, and pointers into
7-
/// other pointers.
6+
/// uses that include turning pointers into addresses, addresses into pointers, pointers into
7+
/// other pointers, and qualifying paths for associated items.
88
///
99
/// ```rust
1010
/// let thing1: u8 = 89.0 as u8;
@@ -37,9 +37,25 @@
3737
/// use std::{mem as memory, net as network};
3838
/// // Now you can use the names `memory` and `network` to refer to `std::mem` and `std::net`.
3939
/// ```
40-
/// For more information on what `as` is capable of, see the [Reference].
41-
///
42-
/// [Reference]: ../reference/expressions/operator-expr.html#type-cast-expressions
40+
/// You'll also find with `From` and `Into`, and indeed all traits, that `as` is used for the
41+
/// _fully qualified path_, a means of disambiguating associated items, i.e. functions,
42+
/// constants, and types. For example, if you have a type which implements two traits with identical
43+
/// method names (e.g. `Into::<u32>::into` and `Into::<u64>::into`), you can clarify which method
44+
/// you'll use with `<MyThing as Into<u32>>::into(my_thing)`[^as-use-from]. This is quite verbose,
45+
/// but fortunately, Rust's type inference usually saves you from needing this, although it is
46+
/// occasionally necessary, especially with methods that return a generic type like `Into::into` or
47+
/// static methods. It's more common to use in macros where it can provide necessary hygiene.
48+
///
49+
/// [^as-use-from]: You should probably never use this syntax with `Into` and instead write
50+
/// `T::from(my_thing)`. It just happens that there aren't any great examples for this syntax in
51+
/// the standard library. Also, at time of writing, the compiler tends to suggest fully-qualified
52+
/// paths to fix ambiguous `Into::into` calls, so the example should hopefully be familiar.
53+
///
54+
/// For more information on what `as` is capable of, see the Reference on [type cast expressions]
55+
/// and [qualified paths].
56+
///
57+
/// [type cast expressions]: ../reference/expressions/operator-expr.html#type-cast-expressions
58+
/// [qualified paths]: ../reference/paths.html#qualified-paths
4359
/// [`crate`]: keyword.crate.html
4460
/// [`use`]: keyword.use.html
4561
/// [const-cast]: pointer::cast

0 commit comments

Comments
 (0)