Skip to content

Commit f44e685

Browse files
committed
Changed invalid coersion examples
1 parent 69af035 commit f44e685

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/type-coercions.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ 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

2424
* `static` and `const` items (similar to `let` statements).
@@ -28,13 +28,13 @@ sites are:
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,29 @@ 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
```
5555

56-
* Function results – either the final line of a block if it is not
56+
* Function results–either the final line of a block if it is not
5757
semicolon-terminated or any expression in a `return` statement
5858

59-
For example, `42` is coerced to have type `i8` in the following:
59+
For example, `x` is coerced to have type `&dyn Binary` in the following:
6060

6161
```rust
62-
fn foo() -> i8 {
63-
42
62+
fn foo(x: &u32) -> &dyn Binary {
63+
x
6464
}
6565
```
66+
The [as] keyword can also be used to perform explicit type coersion, as well as
67+
some additional casts.
68+
6669

6770
If the expression in one of these coercion sites is a coercion-propagating
6871
expression, then the relevant sub-expressions in that expression are also
@@ -183,6 +186,7 @@ unsized coercion to `Foo<U>`.
183186
184187
[RFC 401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
185188
[RFC 1558]: https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md
186-
[subtype]: subtyping.html
189+
[subtype]: subtyping.html`
190+
[as]: operator-expr.html#type-cast-expressions`
187191
[`Unsize`]: ../std/marker/trait.Unsize.html
188192
[`CoerceUnsized`]: ../std/ops/trait.CoerceUnsized.html

0 commit comments

Comments
 (0)