1
1
# Type coercions
2
2
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.
6
6
7
7
Coercions are originally defined in [ RFC 401] and expanded upon in [ RFC 1558] .
8
8
@@ -15,26 +15,26 @@ sites are:
15
15
16
16
* ` let ` statements where an explicit type is given.
17
17
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:
19
19
20
20
``` rust
21
- let _ : i8 = 42 ;
21
+ let _ : & i8 = & mut 42 ;
22
22
```
23
23
24
- * ` static ` and ` const ` items (similar to ` let ` statements).
24
+ * ` static ` and ` const ` item declarations (similar to ` let ` statements).
25
25
26
26
* Arguments for function calls
27
27
28
28
The value being coerced is the actual parameter, and it is coerced to
29
29
the type of the formal parameter.
30
30
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:
32
32
33
33
``` rust
34
- fn bar (_ : i8 ) { }
34
+ fn bar (_ : & i8 ) { }
35
35
36
36
fn main () {
37
- bar (42 );
37
+ bar (& mut 42 );
38
38
}
39
39
```
40
40
@@ -43,26 +43,30 @@ sites are:
43
43
44
44
* Instantiations of struct, union, or enum variant fields
45
45
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:
47
47
48
48
``` rust
49
- struct Foo { x : i8 }
49
+ struct Foo { x : & i8 }
50
50
51
51
fn main () {
52
- Foo { x : 42 };
52
+ Foo { x : & mut 42 };
53
53
}
54
54
```
55
+
56
+ (Note that lifetime specifiers on ` struct Foo ` have been omitted for brevity.)
55
57
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
57
59
semicolon-terminated or any expression in a ` return ` statement
58
60
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:
60
62
61
63
``` rust
62
- fn foo () -> i8 {
63
- 42
64
+ fn foo (x : & u32 ) -> & dyn Display {
65
+ x
64
66
}
65
67
```
68
+ * The [ as] type cast operator can also explicitly perform type coersion.
69
+
66
70
67
71
If the expression in one of these coercion sites is a coercion-propagating
68
72
expression, then the relevant sub-expressions in that expression are also
@@ -183,6 +187,7 @@ unsized coercion to `Foo<U>`.
183
187
184
188
[ RFC 401 ] : https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
185
189
[ 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`
187
192
[ `Unsize` ] : ../std/marker/trait.Unsize.html
188
193
[ `CoerceUnsized` ] : ../std/ops/trait.CoerceUnsized.html
0 commit comments