about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authoriirelu <anna@bawk.space>2018-09-03 19:41:01 +0200
committeriirelu <anna@bawk.space>2018-09-03 19:41:01 +0200
commit1142bbdfc4e9ae48045e9b7a2c6b507aa0626e84 (patch)
tree7582db182572675bf71e17647e6eca0a8a2b7bc4 /src/libstd
parent047aac5cc60016b7c7f2a92f335b4a82b07ec59c (diff)
downloadrust-1142bbdfc4e9ae48045e9b7a2c6b507aa0626e84.tar.gz
rust-1142bbdfc4e9ae48045e9b7a2c6b507aa0626e84.zip
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.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/keyword_docs.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/libstd/keyword_docs.rs b/src/libstd/keyword_docs.rs
index b5593e44f8b..db447c1b363 100644
--- a/src/libstd/keyword_docs.rs
+++ b/src/libstd/keyword_docs.rs
@@ -8,6 +8,33 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#[doc(keyword = "as")]
+//
+/// The type coercion keyword
+///
+/// `as` is most commonly used to turn primitive types into other primitive types, but it has other
+/// uses that include turning pointers into addresses, addresses into pointers, and pointers into
+/// other pointers.
+///
+/// ```rust
+/// let thing1: u8 = 89.0 as u8;
+/// assert_eq!('B' as u32, 66);
+/// assert_eq!(thing1 as char, 'Y');
+/// let thing2: f32 = thing1 as f32 + 10.5;
+/// assert_eq!(true as u8 + thing2 as u8, 100);
+/// ```
+///
+/// In general, any coercion that can be performed via writing out type hints can also be done
+/// using `as`, so instead of writing `let x: u32 = 123`, you can write `let x = 123 as u32` (Note:
+/// `let x = 123u32` would be best in that situation). The same is not true in the other direction,
+/// however, explicitly using `as` allows a few more coercions that aren't allowed implicitly, such
+/// as changing the type of a raw pointer or turning closures into raw pointers.
+///
+/// For more information on what `as` is capable of, see the [Reference]
+///
+/// [Reference]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
+mod as_keyword { }
+
 #[doc(keyword = "fn")]
 //
 /// The `fn` keyword.