Skip to content

Commit eee2cbe

Browse files
authored
Rollup merge of rust-lang#142670 - fpdotmonkey:doc/as-fully-qualified-syntax, r=ibraheemdev,fmease
Document fully-qualified syntax in `as`' keyword doc
2 parents 7950f24 + 1485e78 commit eee2cbe

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

library/std/src/keyword_docs.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#[doc(keyword = "as")]
22
//
3-
/// Cast between types, or rename an import.
3+
/// Cast between types, rename an import, or qualify paths to associated items.
4+
///
5+
/// # Type casting
46
///
57
/// `as` is most commonly used to turn primitive types into other primitive types, but it has other
68
/// uses that include turning pointers into addresses, addresses into pointers, and pointers into
@@ -30,16 +32,43 @@
3032
/// `as *mut _` though the [`cast`][const-cast] method is recommended over `as *const _` and it is
3133
/// [the same][mut-cast] for `as *mut _`: those methods make the intent clearer.
3234
///
35+
/// # Renaming imports
36+
///
3337
/// `as` is also used to rename imports in [`use`] and [`extern crate`][`crate`] statements:
3438
///
3539
/// ```
3640
/// # #[allow(unused_imports)]
3741
/// use std::{mem as memory, net as network};
3842
/// // Now you can use the names `memory` and `network` to refer to `std::mem` and `std::net`.
3943
/// ```
40-
/// For more information on what `as` is capable of, see the [Reference].
4144
///
42-
/// [Reference]: ../reference/expressions/operator-expr.html#type-cast-expressions
45+
/// # Qualifying paths
46+
///
47+
/// You'll also find with `From` and `Into`, and indeed all traits, that `as` is used for the
48+
/// _fully qualified path_, a means of disambiguating associated items, i.e. functions,
49+
/// constants, and types. For example, if you have a type which implements two traits with identical
50+
/// method names (e.g. `Into::<u32>::into` and `Into::<u64>::into`), you can clarify which method
51+
/// you'll use with `<MyThing as Into<u32>>::into(my_thing)`[^as-use-from]. This is quite verbose,
52+
/// but fortunately, Rust's type inference usually saves you from needing this, although it is
53+
/// occasionally necessary, especially with methods that return a generic type like `Into::into` or
54+
/// methods that don't take `self`. It's more common to use in macros where it can provide necessary
55+
/// hygiene.
56+
///
57+
/// [^as-use-from]: You should probably never use this syntax with `Into` and instead write
58+
/// `T::from(my_thing)`. It just happens that there aren't any great examples for this syntax in
59+
/// the standard library. Also, at time of writing, the compiler tends to suggest fully-qualified
60+
/// paths to fix ambiguous `Into::into` calls, so the example should hopefully be familiar.
61+
///
62+
/// # Further reading
63+
///
64+
/// For more information on what `as` is capable of, see the Reference on [type cast expressions],
65+
/// [renaming imported entities], [renaming `extern` crates]
66+
/// and [qualified paths].
67+
///
68+
/// [type cast expressions]: ../reference/expressions/operator-expr.html#type-cast-expressions
69+
/// [renaming imported entities]: https://doc.rust-lang.org/reference/items/use-declarations.html#as-renames
70+
/// [renaming `extern` crates]: https://doc.rust-lang.org/reference/items/extern-crates.html#r-items.extern-crate.as
71+
/// [qualified paths]: ../reference/paths.html#qualified-paths
4372
/// [`crate`]: keyword.crate.html
4473
/// [`use`]: keyword.use.html
4574
/// [const-cast]: pointer::cast

0 commit comments

Comments
 (0)