1
1
# Type coercions
2
2
3
- Coercions are defined in [ RFC 401] . [ RFC 1558] then expanded on that.
4
- A coercion is implicit and has no syntax.
3
+ ** Type coercions** are implicit operations that change the type of a value.
4
+ They happen automatically at specific locations and are highly restricted in
5
+ what types actually coerce.
5
6
6
- [ RFC 401 ] : https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
7
- [ RFC 1558 ] : https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md
7
+ Any conversions allowed by coercion can also be explicitly performed by the
8
+ [ type cast operator] , ` as ` .
9
+
10
+ Coercions are originally defined in [ RFC 401] and expanded upon in [ RFC 1558] .
8
11
9
12
## Coercion sites
10
13
@@ -15,52 +18,53 @@ sites are:
15
18
16
19
* ` let ` statements where an explicit type is given.
17
20
18
- For example, ` 42 ` is coerced to have type ` i8 ` in the following:
21
+ For example, ` &mut 42` is coerced to have type ` & i8` in the following:
19
22
20
23
``` rust
21
- let _ : i8 = 42 ;
24
+ let _ : & i8 = & mut 42 ;
22
25
```
23
26
24
- * ` static ` and ` const ` statements (similar to ` let ` statements).
27
+ * ` static ` and ` const ` item declarations (similar to ` let ` statements).
25
28
26
29
* Arguments for function calls
27
30
28
31
The value being coerced is the actual parameter, and it is coerced to
29
32
the type of the formal parameter.
30
33
31
- For example, ` 42 ` is coerced to have type ` i8 ` in the following:
34
+ For example, ` &mut 42` is coerced to have type ` & i8` in the following:
32
35
33
36
``` rust
34
- fn bar (_ : i8 ) { }
37
+ fn bar (_ : & i8 ) { }
35
38
36
39
fn main () {
37
- bar (42 );
40
+ bar (& mut 42 );
38
41
}
39
42
```
40
43
41
44
For method calls, the receiver (` self ` parameter) can only take advantage
42
45
of [ unsized coercions] ( #unsized-coercions ) .
43
46
44
- * Instantiations of struct or variant fields
47
+ * Instantiations of struct, union, or enum variant fields
45
48
46
- For example, ` 42 ` is coerced to have type ` i8 ` in the following:
49
+ For example, ` &mut 42` is coerced to have type ` & i8` in the following:
47
50
48
51
``` rust
49
- struct Foo { x : i8 }
52
+ struct Foo <' a > { x : & ' a i8 }
50
53
51
54
fn main () {
52
- Foo { x : 42 };
55
+ Foo { x : & mut 42 };
53
56
}
54
57
```
55
-
56
- * Function results, either the final line of a block if it is not
58
+
59
+ * Function results& mdash ; either the final line of a block if it is not
57
60
semicolon-terminated or any expression in a ` return ` statement
58
61
59
- For example, ` 42 ` is coerced to have type ` i8 ` in the following:
62
+ For example, ` x ` is coerced to have type ` &dyn Display ` in the following:
60
63
61
64
``` rust
62
- fn foo () -> i8 {
63
- 42
65
+ use std :: fmt :: Display ;
66
+ fn foo (x : & u32 ) -> & dyn Display {
67
+ x
64
68
}
65
69
```
66
70
@@ -91,7 +95,7 @@ the block has a known type.
91
95
92
96
Coercion is allowed between the following types:
93
97
94
- * ` T ` to ` U ` if ` T ` is a subtype of ` U ` (* reflexive case* )
98
+ * ` T ` to ` U ` if ` T ` is a [ subtype] of ` U ` (* reflexive case* )
95
99
96
100
* ` T_1 ` to ` T_3 ` where ` T_1 ` coerces to ` T_2 ` and ` T_2 ` coerces to ` T_3 `
97
101
(* transitive case* )
@@ -164,8 +168,7 @@ an implementation of `Unsize<U>` for `T` will be provided:
164
168
165
169
* ` [T; n] ` to ` [T] ` .
166
170
167
- * ` T ` to ` U ` , when ` U ` is a trait object type and either ` T ` implements ` U ` or
168
- ` T ` is a trait object for a subtrait of ` U ` .
171
+ * ` T ` to ` dyn U ` , when ` T ` implements ` U + Sized ` , and ` U ` is [ object safe] .
169
172
170
173
* ` Foo<..., T, ...> ` to ` Foo<..., U, ...> ` , when:
171
174
* ` Foo ` is a struct.
@@ -182,5 +185,10 @@ unsized coercion to `Foo<U>`.
182
185
> has been stabilized, the traits themselves are not yet stable and therefore
183
186
> can't be used directly in stable Rust.
184
187
188
+ [ RFC 401 ] : https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
189
+ [ RFC 1558 ] : https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md
190
+ [ subtype ] : subtyping.md
191
+ [ object safe ] : items/traits.md#object-safety
192
+ [ type cast operator ] : expressions/operator-expr.md#type-cast-expressions
185
193
[ `Unsize` ] : ../std/marker/trait.Unsize.html
186
194
[ `CoerceUnsized` ] : ../std/ops/trait.CoerceUnsized.html
0 commit comments