@@ -7,9 +7,12 @@ most dangerous features of Rust!
7
7
8
8
# Coercion
9
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.
10
+ Coercion between types is implicit and has no syntax of its own, but can
11
+ be spelled out with [ ` as ` ] ( #explicit-coercions ) .
12
+
13
+ Coercion occurs in ` let ` , ` const ` , and ` static ` statements; in
14
+ function call arguments; in field values in struct initialization; and in a
15
+ function result.
13
16
14
17
The main cases of coercion are:
15
18
@@ -21,6 +24,9 @@ The main cases of coercion are:
21
24
22
25
* ` &mut T ` to ` *mut T `
23
26
27
+ * A custom coercion using [ ` Deref ` ] ( deref-coercions.md )
28
+
29
+
24
30
# ` as `
25
31
26
32
The ` as ` keyword does safe casting:
@@ -50,15 +56,14 @@ let a = "hello";
50
56
let b = a as String ;
51
57
```
52
58
53
- All coercions will be made implicitly when necessary and unambiguous.
54
-
55
59
## Numeric casts
56
60
57
61
A cast ` e as U ` is also valid in any of the following cases:
58
62
59
63
* ` e ` has type ` T ` and ` T ` and ` U ` are any numeric types; * numeric-cast*
60
- * ` e ` is a C-like enum and ` U ` is an integer type; * enum-cast*
61
- * ` e ` has type ` bool ` or ` char ` and ` U ` is an integer; * prim-int-cast*
64
+ * ` e ` is a C-like enum (with no data attached to the variants),
65
+ and ` U ` is an integer type; * enum-cast*
66
+ * ` e ` has type ` bool ` or ` char ` and ` U ` is an integer type; * prim-int-cast*
62
67
* ` e ` has type ` u8 ` and ` U ` is ` char ` ; * u8-char-cast*
63
68
64
69
For example
@@ -68,7 +73,7 @@ let one = true as u8;
68
73
let at_sign = 64 as char ;
69
74
```
70
75
71
- For numeric casts, there are quite a few cases to consider :
76
+ The semantics of numeric casts are:
72
77
73
78
* Casting between two integers of the same size (e.g. i32 -> u32) is a no-op
74
79
* Casting from a larger integer to a smaller integer (e.g. u32 -> u8) will
@@ -100,13 +105,20 @@ Perhaps surprisingly, it is safe to cast pointers to and from integers, and
100
105
to cast between pointers to different types subject to some constraints. It
101
106
is only unsafe to dereference the pointer.
102
107
108
+ ` e as U ` is a valid pointer cast in any of the following cases:
109
+
103
110
* ` e ` has type ` *T ` , ` U ` has type ` *U_0 ` , and either ` U_0: Sized ` or
104
- unsize_kind(` T ` ) = unsize_kind(` U_0 ` ); a * ptr-ptr-cast*
111
+ ` unsize_kind(T) == unsize_kind(U_0) ` ; a * ptr-ptr-cast*
112
+
105
113
* ` e ` has type ` *T ` and ` U ` is a numeric type, while ` T: Sized ` ; * ptr-addr-cast*
114
+
106
115
* ` e ` is an integer and ` U ` is ` *U_0 ` , while ` U_0: Sized ` ; * addr-ptr-cast*
116
+
107
117
* ` e ` has type ` &[T; n] ` and ` U ` is ` *const T ` ; * array-ptr-cast*
118
+
108
119
* ` e ` is a function pointer type and ` U ` has type ` *T ` ,
109
120
while ` T: Sized ` ; * fptr-ptr-cast*
121
+
110
122
* ` e ` is a function pointer type and ` U ` is an integer; * fptr-addr-cast*
111
123
112
124
0 commit comments