|
1 | 1 | #[doc(keyword = "as")]
|
2 | 2 | //
|
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 |
4 | 6 | ///
|
5 | 7 | /// `as` is most commonly used to turn primitive types into other primitive types, but it has other
|
6 | 8 | /// uses that include turning pointers into addresses, addresses into pointers, and pointers into
|
|
30 | 32 | /// `as *mut _` though the [`cast`][const-cast] method is recommended over `as *const _` and it is
|
31 | 33 | /// [the same][mut-cast] for `as *mut _`: those methods make the intent clearer.
|
32 | 34 | ///
|
| 35 | +/// # Renaming imports |
| 36 | +/// |
33 | 37 | /// `as` is also used to rename imports in [`use`] and [`extern crate`][`crate`] statements:
|
34 | 38 | ///
|
35 | 39 | /// ```
|
36 | 40 | /// # #[allow(unused_imports)]
|
37 | 41 | /// use std::{mem as memory, net as network};
|
38 | 42 | /// // Now you can use the names `memory` and `network` to refer to `std::mem` and `std::net`.
|
39 | 43 | /// ```
|
40 |
| -/// For more information on what `as` is capable of, see the [Reference]. |
41 | 44 | ///
|
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 |
43 | 72 | /// [`crate`]: keyword.crate.html
|
44 | 73 | /// [`use`]: keyword.use.html
|
45 | 74 | /// [const-cast]: pointer::cast
|
|
0 commit comments