|
20 | 20 | /// explicitly using `as` allows a few more coercions that aren't allowed implicitly, such as |
21 | 21 | /// changing the type of a raw pointer or turning closures into raw pointers. |
22 | 22 | /// |
23 | | -/// `as` is also used to rename imports in [`use`] and [`extern crate`] statements: |
| 23 | +/// `as` can be seen as the primitive for `From` and `Into`: `as` only works with primitives |
| 24 | +/// (`u8`, `bool`, `str`, pointers, ...) whereas `From` and `Into` also works with types like |
| 25 | +/// `String` or `Vec`. |
| 26 | +/// |
| 27 | +/// `as` can also be used with the `_` placeholder when the destination type can be inferred. Note |
| 28 | +/// that this can cause inference breakage and usually such code should use an explicit type for |
| 29 | +/// both clarity and stability. This is most useful when converting pointers using `as *const _` or |
| 30 | +/// `as *mut _` though the [`cast`][const-cast] method is recommended over `as *const _` and it is |
| 31 | +/// [the same][mut-cast] for `as *mut _`: those methods make the intent clearer. |
| 32 | +/// |
| 33 | +/// `as` is also used to rename imports in [`use`] and [`extern crate`][`crate`] statements: |
24 | 34 | /// |
25 | 35 | /// ``` |
26 | 36 | /// # #[allow(unused_imports)] |
27 | 37 | /// use std::{mem as memory, net as network}; |
28 | 38 | /// // Now you can use the names `memory` and `network` to refer to `std::mem` and `std::net`. |
29 | 39 | /// ``` |
30 | | -/// |
31 | 40 | /// For more information on what `as` is capable of, see the [Reference]. |
32 | 41 | /// |
33 | 42 | /// [Reference]: ../reference/expressions/operator-expr.html#type-cast-expressions |
| 43 | +/// [`crate`]: keyword.crate.html |
34 | 44 | /// [`use`]: keyword.use.html |
35 | | -/// [`extern crate`]: keyword.crate.html |
| 45 | +/// [const-cast]: primitive.pointer.html#method.cast |
| 46 | +/// [mut-cast]: primitive.pointer.html#method.cast-1 |
36 | 47 | mod as_keyword {} |
37 | 48 |
|
38 | 49 | #[doc(keyword = "break")] |
|
0 commit comments