Skip to content

Commit 69065f4

Browse files
authored
fix: keyword_docs.rs
close #86287
1 parent d51b6f9 commit 69065f4

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

library/std/src/keyword_docs.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#[doc(keyword = "as")]
22
//
3-
/// Cast between types, or rename an import.
3+
/// The `as` keyword is used in many syntactic locations:
4+
/// * `as` is used to [cast between types][Reference],
5+
/// * `as` is used to rename an import in [`use`] and [`extern crate`][`crate`] statements,
6+
/// * `as` is also used to [disambiguate trait implementations][Disambiguation]
47
///
5-
/// `as` is most commonly used to turn primitive types into other primitive types, but it has other
8+
/// In the type-casting sense, `as` is most commonly used to turn primitive types into other primitive types, but it has other
69
/// uses that include turning pointers into addresses, addresses into pointers, and pointers into
710
/// other pointers.
811
///
@@ -14,7 +17,7 @@
1417
/// assert_eq!(true as u8 + thing2 as u8, 100);
1518
/// ```
1619
///
17-
/// In general, any cast that can be performed via ascribing the type can also be done using `as`,
20+
/// In general, any type-cast that can be performed via ascribing the type can also be done using `as`,
1821
/// so instead of writing `let x: u32 = 123`, you can write `let x = 123 as u32` (note: `let x: u32
1922
/// = 123` would be best in that situation). The same is not true in the other direction, however;
2023
/// explicitly using `as` allows a few more coercions that aren't allowed implicitly, such as
@@ -37,9 +40,32 @@
3740
/// use std::{mem as memory, net as network};
3841
/// // Now you can use the names `memory` and `network` to refer to `std::mem` and `std::net`.
3942
/// ```
43+
///
44+
/// `as` is also used to [disambiguate trait implementations][Disambiguate]:
45+
///
46+
/// ```
47+
/// struct S;
48+
/// impl S {
49+
/// fn f() { println!("S"); }
50+
/// }
51+
/// trait T1 {
52+
/// fn f() { println!("T1 f"); }
53+
/// }
54+
/// impl T1 for S {}
55+
/// trait T2 {
56+
/// fn f() { println!("T2 f"); }
57+
/// }
58+
/// impl T2 for S {}
59+
/// S::f(); // Calls the inherent impl.
60+
/// <S as T1>::f(); // Calls the T1 trait function.
61+
/// <S as T2>::f(); // Calls the T2 trait function.
62+
///
63+
/// ```
64+
///
4065
/// For more information on what `as` is capable of, see the [Reference].
4166
///
4267
/// [Reference]: ../reference/expressions/operator-expr.html#type-cast-expressions
68+
/// [Disambiguation]: ../reference/paths.html#r-paths.qualified.intro
4369
/// [`crate`]: keyword.crate.html
4470
/// [`use`]: keyword.use.html
4571
/// [const-cast]: pointer::cast

0 commit comments

Comments
 (0)