@@ -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 coercions may be defined using [ ` Deref ` ] ( deref-coercions.md ) .
33
+
34
+ Coercion is transitive.
29
35
30
36
# ` as `
31
37
@@ -64,6 +70,7 @@ For example
64
70
``` rust
65
71
let one = true as u8 ;
66
72
let at_sign = 64 as char ;
73
+ let two_hundred = - 56i8 as u8 ;
67
74
```
68
75
69
76
The semantics of numeric casts are:
@@ -94,9 +101,14 @@ The semantics of numeric casts are:
94
101
95
102
## Pointer casts
96
103
97
- Perhaps surprisingly, it is safe to cast pointers to and from integers, and
98
- to cast between pointers to different types subject to some constraints. It
99
- is only unsafe to dereference the pointer.
104
+ Perhaps surprisingly, it is safe to cast [ raw pointers] ( raw-pointers.md ) to and
105
+ from integers, and to cast between pointers to different types subject to
106
+ some constraints. It is only unsafe to dereference the pointer:
107
+
108
+ ``` rust
109
+ let a = 300 as * const char ; // a pointer to location 300
110
+ let b = a as u32 ;
111
+ ```
100
112
101
113
` e as U ` is a valid pointer cast in any of the following cases:
102
114
0 commit comments