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 changes of the type of a value. They happen
4
+ automatically at specific locations and are highly restricted in what types
5
+ 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
+ Coercions are originally defined in [ RFC 401] and expanded upon in [ RFC 1558] .
8
8
9
9
## Coercion sites
10
10
@@ -21,7 +21,7 @@ sites are:
21
21
let _ : i8 = 42 ;
22
22
```
23
23
24
- * ` static ` and ` const ` statements (similar to ` let ` statements).
24
+ * ` static ` and ` const ` items (similar to ` let ` statements).
25
25
26
26
* Arguments for function calls
27
27
@@ -41,7 +41,7 @@ sites are:
41
41
For method calls, the receiver (` self ` parameter) can only take advantage
42
42
of [ unsized coercions] ( #unsized-coercions ) .
43
43
44
- * Instantiations of struct or variant fields
44
+ * Instantiations of struct, union, or enum variant fields
45
45
46
46
For example, ` 42 ` is coerced to have type ` i8 ` in the following:
47
47
@@ -53,7 +53,7 @@ sites are:
53
53
}
54
54
```
55
55
56
- * Function results, either the final line of a block if it is not
56
+ * Function results & mdash ; either the final line of a block if it is not
57
57
semicolon-terminated or any expression in a ` return ` statement
58
58
59
59
For example, ` 42 ` is coerced to have type ` i8 ` in the following:
@@ -91,7 +91,7 @@ the block has a known type.
91
91
92
92
Coercion is allowed between the following types:
93
93
94
- * ` T ` to ` U ` if ` T ` is a subtype of ` U ` (* reflexive case* )
94
+ * ` T ` to ` U ` if ` T ` is a [ subtype] of ` U ` (* reflexive case* )
95
95
96
96
* ` T_1 ` to ` T_3 ` where ` T_1 ` coerces to ` T_2 ` and ` T_2 ` coerces to ` T_3 `
97
97
(* transitive case* )
@@ -140,7 +140,7 @@ Coercion is allowed between the following types:
140
140
- ` *mut T `
141
141
- ` Box<T> `
142
142
143
- and where ` T ` can obtained from ` U ` by [ unsized coercion] ( #unsized-coercions ) .
143
+ and where ` T ` can obtained from ` U ` by an [ unsized coercion] ( #unsized-coercions ) .
144
144
145
145
<!-- In the future, coerce_inner will be recursively extended to tuples and
146
146
structs. In addition, coercions from sub-traits to super-traits will be
@@ -164,8 +164,7 @@ the compiler will provide an implementation of `Unsize<U>` for `T`:
164
164
165
165
* ` [T; n] ` to ` [T] ` .
166
166
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 ` .
167
+ * ` T ` to ` dyn U ` , when ` T ` implements ` U + Sized `
169
168
170
169
* ` Foo<..., T, ...> ` to ` Foo<..., U, ...> ` , when:
171
170
* ` Foo ` is a struct.
@@ -182,5 +181,8 @@ unsized coercion to `Foo<U>`.
182
181
> has been stabilized, the traits themselves are not yet stable and therefore
183
182
> can't be used directly in stable Rust.
184
183
185
- [ Unsize ] : ../std/marker/trait.Unsize.html
186
- [ CoerceUnsized ] : ../std/ops/trait.CoerceUnsized.html
184
+ [ RFC 401 ] : https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
185
+ [ RFC 1558 ] : https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md
186
+ [ subtype ] : subtyping.html
187
+ [ `Unsize` ] : ../std/marker/trait.Unsize.html
188
+ [ `CoerceUnsized` ] : ../std/ops/trait.CoerceUnsized.html
0 commit comments