Skip to content

Commit 1142bbd

Browse files
committed
Add docs for as keyword
It's pretty basic and could do with more details, but it's a good starter until someone else improves it.
1 parent 047aac5 commit 1142bbd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/libstd/keyword_docs.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[doc(keyword = "as")]
12+
//
13+
/// The type coercion keyword
14+
///
15+
/// `as` is most commonly used to turn primitive types into other primitive types, but it has other
16+
/// uses that include turning pointers into addresses, addresses into pointers, and pointers into
17+
/// other pointers.
18+
///
19+
/// ```rust
20+
/// let thing1: u8 = 89.0 as u8;
21+
/// assert_eq!('B' as u32, 66);
22+
/// assert_eq!(thing1 as char, 'Y');
23+
/// let thing2: f32 = thing1 as f32 + 10.5;
24+
/// assert_eq!(true as u8 + thing2 as u8, 100);
25+
/// ```
26+
///
27+
/// In general, any coercion that can be performed via writing out type hints can also be done
28+
/// using `as`, so instead of writing `let x: u32 = 123`, you can write `let x = 123 as u32` (Note:
29+
/// `let x = 123u32` would be best in that situation). The same is not true in the other direction,
30+
/// however, explicitly using `as` allows a few more coercions that aren't allowed implicitly, such
31+
/// as changing the type of a raw pointer or turning closures into raw pointers.
32+
///
33+
/// For more information on what `as` is capable of, see the [Reference]
34+
///
35+
/// [Reference]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
36+
mod as_keyword { }
37+
1138
#[doc(keyword = "fn")]
1239
//
1340
/// The `fn` keyword.

0 commit comments

Comments
 (0)