Skip to content

Commit 281d576

Browse files
authored
Merge pull request #843 from AloeareV/coercions
Improve Type-Coersion Documentation
2 parents d27d801 + d5372c9 commit 281d576

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/type-coercions.md

+30-22
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
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 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.
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+
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].
811

912
## Coercion sites
1013

@@ -15,52 +18,53 @@ sites are:
1518

1619
* `let` statements where an explicit type is given.
1720

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:
1922

2023
```rust
21-
let _: i8 = 42;
24+
let _: &i8 = &mut 42;
2225
```
2326

24-
* `static` and `const` statements (similar to `let` statements).
27+
* `static` and `const` item declarations (similar to `let` statements).
2528

2629
* Arguments for function calls
2730

2831
The value being coerced is the actual parameter, and it is coerced to
2932
the type of the formal parameter.
3033

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:
3235

3336
```rust
34-
fn bar(_: i8) { }
37+
fn bar(_: &i8) { }
3538

3639
fn main() {
37-
bar(42);
40+
bar(&mut 42);
3841
}
3942
```
4043

4144
For method calls, the receiver (`self` parameter) can only take advantage
4245
of [unsized coercions](#unsized-coercions).
4346

44-
* Instantiations of struct or variant fields
47+
* Instantiations of struct, union, or enum variant fields
4548

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:
4750

4851
```rust
49-
struct Foo { x: i8 }
52+
struct Foo<'a> { x: &'a i8 }
5053

5154
fn main() {
52-
Foo { x: 42 };
55+
Foo { x: &mut 42 };
5356
}
5457
```
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
5760
semicolon-terminated or any expression in a `return` statement
5861

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:
6063

6164
```rust
62-
fn foo() -> i8 {
63-
42
65+
use std::fmt::Display;
66+
fn foo(x: &u32) -> &dyn Display {
67+
x
6468
}
6569
```
6670

@@ -91,7 +95,7 @@ the block has a known type.
9195

9296
Coercion is allowed between the following types:
9397

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*)
9599

96100
* `T_1` to `T_3` where `T_1` coerces to `T_2` and `T_2` coerces to `T_3`
97101
(*transitive case*)
@@ -164,8 +168,7 @@ an implementation of `Unsize<U>` for `T` will be provided:
164168

165169
* `[T; n]` to `[T]`.
166170

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].
169172

170173
* `Foo<..., T, ...>` to `Foo<..., U, ...>`, when:
171174
* `Foo` is a struct.
@@ -182,5 +185,10 @@ unsized coercion to `Foo<U>`.
182185
> has been stabilized, the traits themselves are not yet stable and therefore
183186
> can't be used directly in stable Rust.
184187
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
185193
[`Unsize`]: ../std/marker/trait.Unsize.html
186194
[`CoerceUnsized`]: ../std/ops/trait.CoerceUnsized.html

0 commit comments

Comments
 (0)