|
24 | 24 | /// (`u8`, `bool`, `str`, pointers, ...) whereas `From` and `Into` also works with types like
|
25 | 25 | /// `String` or `Vec`.
|
26 | 26 | ///
|
| 27 | +/// You'll also find with `From` and `Into`, and indeed all traits, that `as` is used for the |
| 28 | +/// _fully qualified path_, a means of clarifying ambiguous associated items, i.e. functions, |
| 29 | +/// constants, and types. For example, if you have a type which implements two traits with identical |
| 30 | +/// method names (e.g. `Into::<u32>::into` and `Into::<u64>::into`), you can clarify which method |
| 31 | +/// you'll use with `<MyThing as Into<u32>>::into(my_thing)`[^as-use-from]. This is quite verbose, |
| 32 | +/// but fortunately, Rust's type inference usually saves you from needing this, although it is |
| 33 | +/// occasionally necessary, especially with methods that return a generic type like `Into::into` or |
| 34 | +/// static methods. It's more common to use in macros where it can provide necessary hygeine. |
| 35 | +/// |
| 36 | +/// [^as-use-from]: You should probably never use this syntax with `Into` and instead write |
| 37 | +/// `T::from(my_thing)`. It just happens that there aren't any great examples for this syntax in |
| 38 | +/// the standard library. Also, at time of writing, the compiler tends to suggest fully-qualified |
| 39 | +/// paths to fix ambiguous `Into::into` calls, so the example should hopefully be familiar. |
| 40 | +/// |
27 | 41 | /// `as` can also be used with the `_` placeholder when the destination type can be inferred. Note
|
28 | 42 | /// that this can cause inference breakage and usually such code should use an explicit type for
|
29 | 43 | /// both clarity and stability. This is most useful when converting pointers using `as *const _` or
|
|
0 commit comments