Skip to content

Commit 797d543

Browse files
committed
Rephrased description of casting
1 parent 34e6995 commit 797d543

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/doc/book/casting-between-types.md

+21-9
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ most dangerous features of Rust!
77

88
# Coercion
99

10-
Coercion between types is implicit and has no explicit syntax. Coercion occurs
11-
in `let`, `const`, and `static` statements; in function call arguments; in
12-
field values in struct initialization; and in a function result.
10+
Coercion between types is implicit and has no syntax of its own, but can
11+
be spelled out with [`as`](#explicit-coercions).
12+
13+
Coercion occurs in `let`, `const`, and `static` statements; in
14+
function call arguments; in field values in struct initialization; and in a
15+
function result.
1316

1417
The main cases of coercion are:
1518

@@ -21,6 +24,9 @@ The main cases of coercion are:
2124

2225
* `&mut T` to `*mut T`
2326

27+
* A custom coercion using [`Deref`](deref-coercions.md)
28+
29+
2430
# `as`
2531

2632
The `as` keyword does safe casting:
@@ -50,15 +56,14 @@ let a = "hello";
5056
let b = a as String;
5157
```
5258

53-
All coercions will be made implicitly when necessary and unambiguous.
54-
5559
## Numeric casts
5660

5761
A cast `e as U` is also valid in any of the following cases:
5862

5963
* `e` has type `T` and `T` and `U` are any numeric types; *numeric-cast*
60-
* `e` is a C-like enum and `U` is an integer type; *enum-cast*
61-
* `e` has type `bool` or `char` and `U` is an integer; *prim-int-cast*
64+
* `e` is a C-like enum (with no data attached to the variants),
65+
and `U` is an integer type; *enum-cast*
66+
* `e` has type `bool` or `char` and `U` is an integer type; *prim-int-cast*
6267
* `e` has type `u8` and `U` is `char`; *u8-char-cast*
6368

6469
For example
@@ -68,7 +73,7 @@ let one = true as u8;
6873
let at_sign = 64 as char;
6974
```
7075

71-
For numeric casts, there are quite a few cases to consider:
76+
The semantics of numeric casts are:
7277

7378
* Casting between two integers of the same size (e.g. i32 -> u32) is a no-op
7479
* Casting from a larger integer to a smaller integer (e.g. u32 -> u8) will
@@ -100,13 +105,20 @@ Perhaps surprisingly, it is safe to cast pointers to and from integers, and
100105
to cast between pointers to different types subject to some constraints. It
101106
is only unsafe to dereference the pointer.
102107

108+
`e as U` is a valid pointer cast in any of the following cases:
109+
103110
* `e` has type `*T`, `U` has type `*U_0`, and either `U_0: Sized` or
104-
unsize_kind(`T`) = unsize_kind(`U_0`); a *ptr-ptr-cast*
111+
`unsize_kind(T) == unsize_kind(U_0)`; a *ptr-ptr-cast*
112+
105113
* `e` has type `*T` and `U` is a numeric type, while `T: Sized`; *ptr-addr-cast*
114+
106115
* `e` is an integer and `U` is `*U_0`, while `U_0: Sized`; *addr-ptr-cast*
116+
107117
* `e` has type `&[T; n]` and `U` is `*const T`; *array-ptr-cast*
118+
108119
* `e` is a function pointer type and `U` has type `*T`,
109120
while `T: Sized`; *fptr-ptr-cast*
121+
110122
* `e` is a function pointer type and `U` is an integer; *fptr-addr-cast*
111123

112124

0 commit comments

Comments
 (0)