@@ -5,6 +5,22 @@ different types between each other. The first, `as`, is for safe casts.
5
5
In contrast, ` transmute ` allows for arbitrary casting, and is one of the
6
6
most dangerous features of Rust!
7
7
8
+ # Coercion
9
+
10
+ Coercion between types is implicit and has no explicit syntax. Coercion occurs
11
+ in ` let ` , ` const ` , and ` static ` statements; in function call arguments; in
12
+ field values in struct initialization; and in a function result.
13
+
14
+ The main cases of coercion are:
15
+
16
+ * ` &mut T ` to ` &T `
17
+
18
+ * ` *mut T ` to ` *const T `
19
+
20
+ * ` &T ` to ` *const T `
21
+
22
+ * ` &mut T ` to ` *mut T `
23
+
8
24
# ` as `
9
25
10
26
The ` as ` keyword does safe casting:
@@ -31,10 +47,10 @@ For example:
31
47
32
48
``` rust
33
49
let a = " hello" ;
34
- let b = a as String
50
+ let b = a as String ;
35
51
```
36
52
37
- Coercions always occur implicitly so this form is only for clarity .
53
+ All coercions will be made implicitly when necessary and unambiguous .
38
54
39
55
## Numeric casts
40
56
@@ -58,7 +74,7 @@ Perhaps surprisingly, it is safe to cast pointers to and from integers, and
58
74
to cast between pointers to different types subject to some constraints. It
59
75
is only unsafe to dereference the pointer.
60
76
61
- * ` e ` has type ` *T ` , ` U ` is a pointer to ` *U_0 ` , and either ` U_0: Sized ` or
77
+ * ` e ` has type ` *T ` , ` U ` has type ` *U_0 ` , and either ` U_0: Sized ` or
62
78
unsize_kind(` T ` ) = unsize_kind(` U_0 ` ); a * ptr-ptr-cast*
63
79
* ` e ` has type ` *T ` and ` U ` is a numeric type, while ` T: Sized ` ; * ptr-addr-cast*
64
80
* ` e ` is an integer and ` U ` is ` *U_0 ` , while ` U_0: Sized ` ; * addr-ptr-cast*
0 commit comments