@@ -14,18 +14,24 @@ Coercion occurs in `let`, `const`, and `static` statements; in
14
14
function call arguments; in field values in struct initialization; and in a
15
15
function result.
16
16
17
- The main cases of coercion are :
17
+ The most common case of coercion is removing mutability from a reference :
18
18
19
19
* ` &mut T ` to ` &T `
20
+
21
+ An analogous conversion is to remove mutability from a
22
+ [ raw pointer] ( raw-pointers.md ) :
20
23
21
24
* ` *mut T ` to ` *const T `
25
+
26
+ References can also be coerced to raw pointers:
22
27
23
28
* ` &T ` to ` *const T `
24
29
25
30
* ` &mut T ` to ` *mut T `
26
-
27
- * A custom coercion using [ ` Deref ` ] ( deref-coercions.md )
28
-
31
+
32
+ Custom coercion may be defined using [ ` Deref ` ] ( deref-coercions.md ) .
33
+
34
+ Coercion is transitive.
29
35
30
36
# ` as `
31
37
@@ -71,6 +77,7 @@ For example
71
77
``` rust
72
78
let one = true as u8 ;
73
79
let at_sign = 64 as char ;
80
+ let two_hundred = - 56i8 as u8 ;
74
81
```
75
82
76
83
The semantics of numeric casts are:
@@ -101,9 +108,14 @@ The semantics of numeric casts are:
101
108
102
109
## Pointer casts
103
110
104
- Perhaps surprisingly, it is safe to cast pointers to and from integers, and
105
- to cast between pointers to different types subject to some constraints. It
106
- is only unsafe to dereference the pointer.
111
+ Perhaps surprisingly, it is safe to cast [ raw pointers] ( raw-pointers.md ) to and
112
+ from integers, and to cast between pointers to different types subject to
113
+ some constraints. It is only unsafe to dereference the pointer:
114
+
115
+ ``` rust
116
+ let a = 300 as * const char ; // a pointer to location 300
117
+ let b = a as u32 ;
118
+ ```
107
119
108
120
` e as U ` is a valid pointer cast in any of the following cases:
109
121
0 commit comments