@@ -7,15 +7,71 @@ most dangerous features of Rust!
7
7
8
8
# ` as `
9
9
10
- The ` as ` keyword does basic casting:
10
+ The ` as ` keyword does safe casting:
11
11
12
12
``` rust
13
13
let x : i32 = 5 ;
14
14
15
15
let y = x as i64 ;
16
16
```
17
17
18
- It only allows certain kinds of casting, however:
18
+ There are three major categories of safe cast: explicit coercions, casts
19
+ between numeric types, and pointer casts.
20
+
21
+ Casting is not transitive: even if ` e as U1 as U2 ` is a valid
22
+ expression, ` e as U2 ` is not necessarily so (in fact it will only be valid if
23
+ ` U1 ` coerces to ` U2 ` ).
24
+
25
+
26
+ ## Explicit coercions
27
+
28
+ A cast ` e as U ` is valid if ` e ` has type ` T ` and ` T ` * coerces* to ` U ` .
29
+
30
+ For example:
31
+
32
+ ``` rust
33
+ let a = " hello" ;
34
+ let b = a as String
35
+ ```
36
+
37
+ Coercions always occur implicitly so this form is only for clarity.
38
+
39
+ ## Numeric casts
40
+
41
+ A cast ` e as U ` is also valid in any of the following cases:
42
+
43
+ * ` e ` has type ` T ` and ` T ` and ` U ` are any numeric types; * numeric-cast*
44
+ * ` e ` is a C-like enum and ` U ` is an integer type; * enum-cast*
45
+ * ` e ` has type ` bool ` or ` char ` and ` U ` is an integer; * prim-int-cast*
46
+ * ` e ` has type ` u8 ` and ` U ` is ` char ` ; * u8-char-cast*
47
+
48
+ For example
49
+
50
+ ``` rust
51
+ let one = true as u8 ;
52
+ let at_sign = 64 as char ;
53
+ ```
54
+
55
+ ## Pointer casts
56
+
57
+ Perhaps surprisingly, it is safe to cast pointers to and from integers, and
58
+ to cast between pointers to different types subject to some constraints. It
59
+ is only unsafe to dereference the pointer.
60
+
61
+ * ` e ` has type ` *T ` , ` U ` is a pointer to ` *U_0 ` , and either ` U_0: Sized ` or
62
+ unsize_kind(` T ` ) = unsize_kind(` U_0 ` ); a * ptr-ptr-cast*
63
+ * ` e ` has type ` *T ` and ` U ` is a numeric type, while ` T: Sized ` ; * ptr-addr-cast*
64
+ * ` e ` is an integer and ` U ` is ` *U_0 ` , while ` U_0: Sized ` ; * addr-ptr-cast*
65
+ * ` e ` has type ` &[T; n] ` and ` U ` is ` *const T ` ; * array-ptr-cast*
66
+ * ` e ` is a function pointer type and ` U ` has type ` *T ` ,
67
+ while ` T: Sized ` ; * fptr-ptr-cast*
68
+ * ` e ` is a function pointer type and ` U ` is an integer; * fptr-addr-cast*
69
+
70
+
71
+ # ` transmute `
72
+
73
+ ` as ` only allows safe casting, and will for example reject an attempt to
74
+ cast four bytes into a ` u32 ` :
19
75
20
76
``` rust,ignore
21
77
let a = [0u8, 0u8, 0u8, 0u8];
@@ -31,13 +87,11 @@ let b = a as u32; // four eights makes 32
31
87
^~~~~~~~
32
88
```
33
89
34
- It’s a ‘non-scalar cast’ because we have multiple values here: the four
90
+ This is a ‘non-scalar cast’ because we have multiple values here: the four
35
91
elements of the array. These kinds of casts are very dangerous, because they
36
92
make assumptions about the way that multiple underlying structures are
37
93
implemented. For this, we need something more dangerous.
38
94
39
- # ` transmute `
40
-
41
95
The ` transmute ` function is provided by a [ compiler intrinsic] [ intrinsics ] , and
42
96
what it does is very simple, but very scary. It tells Rust to treat a value of
43
97
one type as though it were another type. It does this regardless of the
0 commit comments