11# Type coercions
22
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.
56
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] .
88
99## Coercion sites
1010
@@ -21,7 +21,7 @@ sites are:
2121 let _ : i8 = 42 ;
2222 ```
2323
24- * ` static ` and ` const ` statements (similar to ` let ` statements).
24+ * ` static ` and ` const ` items (similar to ` let ` statements).
2525
2626* Arguments for function calls
2727
@@ -41,7 +41,7 @@ sites are:
4141 For method calls, the receiver (` self ` parameter) can only take advantage
4242 of [ unsized coercions] ( #unsized-coercions ) .
4343
44- * Instantiations of struct or variant fields
44+ * Instantiations of struct, union, or enum variant fields
4545
4646 For example, ` 42 ` is coerced to have type ` i8 ` in the following:
4747
@@ -53,7 +53,7 @@ sites are:
5353 }
5454 ```
5555
56- * Function results, either the final line of a block if it is not
56+ * Function results & ndash ; either the final line of a block if it is not
5757 semicolon-terminated or any expression in a ` return ` statement
5858
5959 For example, ` 42 ` is coerced to have type ` i8 ` in the following:
@@ -91,7 +91,7 @@ the block has a known type.
9191
9292Coercion is allowed between the following types:
9393
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* )
9595
9696* ` T_1 ` to ` T_3 ` where ` T_1 ` coerces to ` T_2 ` and ` T_2 ` coerces to ` T_3 `
9797(* transitive case* )
@@ -164,8 +164,7 @@ an implementation of `Unsize<U>` for `T` will be provided:
164164
165165* ` [T; n] ` to ` [T] ` .
166166
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 `
169168
170169* ` Foo<..., T, ...> ` to ` Foo<..., U, ...> ` , when:
171170 * ` Foo ` is a struct.
@@ -182,5 +181,8 @@ unsized coercion to `Foo<U>`.
182181> has been stabilized, the traits themselves are not yet stable and therefore
183182> can't be used directly in stable Rust.
184183
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
185187[ `Unsize` ] : ../std/marker/trait.Unsize.html
186188[ `CoerceUnsized` ] : ../std/ops/trait.CoerceUnsized.html
0 commit comments