11# Type coercions
22
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.
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.
66
77Coercions are originally defined in [ RFC 401] and expanded upon in [ RFC 1558] .
88
@@ -15,26 +15,26 @@ sites are:
1515
1616* ` let ` statements where an explicit type is given.
1717
18- For example, ` 42 ` is coerced to have type ` i8 ` in the following:
18+ For example, ` &mut 42` is coerced to have type ` & i8` in the following:
1919
2020 ``` rust
21- let _ : i8 = 42 ;
21+ let _ : & i8 = & mut 42 ;
2222 ```
2323
24- * ` static ` and ` const ` items (similar to ` let ` statements).
24+ * ` static ` and ` const ` item declarations (similar to ` let ` statements).
2525
2626* Arguments for function calls
2727
2828 The value being coerced is the actual parameter, and it is coerced to
2929 the type of the formal parameter.
3030
31- For example, ` 42 ` is coerced to have type ` i8 ` in the following:
31+ For example, ` &mut 42` is coerced to have type ` & i8` in the following:
3232
3333 ``` rust
34- fn bar (_ : i8 ) { }
34+ fn bar (_ : & i8 ) { }
3535
3636 fn main () {
37- bar (42 );
37+ bar (& mut 42 );
3838 }
3939 ```
4040
@@ -43,26 +43,30 @@ sites are:
4343
4444* Instantiations of struct, union, or enum variant fields
4545
46- For example, ` 42 ` is coerced to have type ` i8 ` in the following:
46+ For example, ` &mut 42` is coerced to have type ` & i8` in the following:
4747
4848 ``` rust
49- struct Foo { x : i8 }
49+ struct Foo { x : & i8 }
5050
5151 fn main () {
52- Foo { x : 42 };
52+ Foo { x : & mut 42 };
5353 }
5454 ```
55+
56+ (Note that lifetime specifiers on ` struct Foo ` have been omitted for brevity.)
5557
56- * Function results &ndash ; either the final line of a block if it is not
58+ * Function results&ndash ; either the final line of a block if it is not
5759 semicolon-terminated or any expression in a ` return ` statement
5860
59- For example, ` 42 ` is coerced to have type ` i8 ` in the following:
61+ For example, ` x ` is coerced to have type ` &dyn Display ` in the following:
6062
6163 ``` rust
62- fn foo () -> i8 {
63- 42
64+ fn foo (x : & u32 ) -> & dyn Display {
65+ x
6466 }
6567 ```
68+ * The [ as] type cast operator can also explicitly perform type coersion.
69+
6670
6771If the expression in one of these coercion sites is a coercion-propagating
6872expression, then the relevant sub-expressions in that expression are also
@@ -183,6 +187,7 @@ unsized coercion to `Foo<U>`.
183187
184188[ RFC 401 ] : https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
185189[ RFC 1558 ] : https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md
186- [ subtype ] : subtyping.html
190+ [ subtype ] : subtyping.html`
191+ [ as ] : operator-expr.html#type-cast-expressions`
187192[ `Unsize` ] : ../std/marker/trait.Unsize.html
188193[ `CoerceUnsized` ] : ../std/ops/trait.CoerceUnsized.html
0 commit comments