Skip to content

Commit 772b1a6

Browse files
Rollup merge of #78086 - poliorcetics:as-placeholder, r=Mark-Simulacrum
Improve doc for 'as _' Fix #78042. `@rustbot` modify labels: A-coercions T-doc
2 parents 4ae328b + e31e627 commit 772b1a6

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

library/std/src/keyword_docs.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,30 @@
2020
/// explicitly using `as` allows a few more coercions that aren't allowed implicitly, such as
2121
/// changing the type of a raw pointer or turning closures into raw pointers.
2222
///
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:
2434
///
2535
/// ```
2636
/// # #[allow(unused_imports)]
2737
/// use std::{mem as memory, net as network};
2838
/// // Now you can use the names `memory` and `network` to refer to `std::mem` and `std::net`.
2939
/// ```
30-
///
3140
/// For more information on what `as` is capable of, see the [Reference].
3241
///
3342
/// [Reference]: ../reference/expressions/operator-expr.html#type-cast-expressions
43+
/// [`crate`]: keyword.crate.html
3444
/// [`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
3647
mod as_keyword {}
3748

3849
#[doc(keyword = "break")]

0 commit comments

Comments
 (0)