You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/expressions/operator-expr.md
+50-41Lines changed: 50 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -331,47 +331,29 @@ Here `*T` means either `*const T` or `*mut T`.
331
331
332
332
### Semantics
333
333
334
-
* Numeric cast
335
-
* Casting between two integers of the same size (e.g. i32 -> u32) is a no-op
336
-
* Casting from a larger integer to a smaller integer (e.g. u32 -> u8) will
337
-
truncate
338
-
* Casting from a smaller integer to a larger integer (e.g. u8 -> u32) will
339
-
* zero-extend if the source is unsigned
340
-
* sign-extend if the source is signed
341
-
* Casting from a float to an integer will round the float towards zero
342
-
*`NaN` will return `0`
343
-
* Values larger than the maximum integer value will saturate to the
344
-
maximum value of the integer type.
345
-
* Values smaller than the minimum integer value will saturate to the
346
-
minimum value of the integer type.
347
-
* Casting from an integer to float will produce the closest possible float \*
348
-
* if necessary, rounding is according to `roundTiesToEven` mode \*\*\*
349
-
* on overflow, infinity (of the same sign as the input) is produced
350
-
* note: with the current set of numeric types, overflow can only happen
351
-
on `u128 as f32` for values greater or equal to `f32::MAX + (0.5 ULP)`
352
-
* Casting from an f32 to an f64 is perfect and lossless
353
-
* Casting from an f64 to an f32 will produce the closest possible f32 \*\*
354
-
* if necessary, rounding is according to `roundTiesToEven` mode \*\*\*
355
-
* on overflow, infinity (of the same sign as the input) is produced
356
-
* Enum cast
357
-
* Casts an enum to its discriminant, then uses a numeric cast if needed.
358
-
* Primitive to integer cast
359
-
*`false` casts to `0`, `true` casts to `1`
360
-
*`char` casts to the value of the code point, then uses a numeric cast if needed.
361
-
*`u8` to `char` cast
362
-
* Casts to the `char` with the corresponding code point.
363
-
* Pointer to address cast
364
-
* Casting from a valid raw pointer to `usize` will produce the address that is pointed to.
365
-
366
-
The pointer's provenance is lost in this conversion.
367
-
* Address to pointer cast
368
-
* Casting from `usize` to a raw pointer will produce a raw pointer to the same location as the original pointer, if the `usize` was obtained through a pointer to address cast of a valid pointer.
369
-
370
-
<divclass="warning">
371
-
Warning:
372
-
The two pointers are not equivalent.
373
-
Dereferencing the pointer obtained from the address to pointer cast may be <ahref="../behavior-considered-undefined.md">undefined behavior</a> if aliasing rules are not followed.
374
-
</div>
334
+
#### Numeric cast
335
+
336
+
* Casting between two integers of the same size (e.g. i32 -> u32) is a no-op
337
+
* Casting from a larger integer to a smaller integer (e.g. u32 -> u8) will
338
+
truncate
339
+
* Casting from a smaller integer to a larger integer (e.g. u8 -> u32) will
340
+
* zero-extend if the source is unsigned
341
+
* sign-extend if the source is signed
342
+
* Casting from a float to an integer will round the float towards zero
343
+
*`NaN` will return `0`
344
+
* Values larger than the maximum integer value will saturate to the
345
+
maximum value of the integer type.
346
+
* Values smaller than the minimum integer value will saturate to the
347
+
minimum value of the integer type.
348
+
* Casting from an integer to float will produce the closest possible float \*
349
+
* if necessary, rounding is according to `roundTiesToEven` mode \*\*\*
350
+
* on overflow, infinity (of the same sign as the input) is produced
351
+
* note: with the current set of numeric types, overflow can only happen
352
+
on `u128 as f32` for values greater or equal to `f32::MAX + (0.5 ULP)`
353
+
* Casting from an f32 to an f64 is perfect and lossless
354
+
* Casting from an f64 to an f32 will produce the closest possible f32 \*\*
355
+
* if necessary, rounding is according to `roundTiesToEven` mode \*\*\*
356
+
* on overflow, infinity (of the same sign as the input) is produced
375
357
376
358
\* if integer-to-float casts with this rounding mode and overflow behavior are
377
359
not supported natively by the hardware, these casts will likely be slower than
@@ -385,6 +367,33 @@ expected.
385
367
number, preferring the one with an even least significant digit if exactly
386
368
halfway between two floating point numbers.
387
369
370
+
#### Enum cast
371
+
372
+
Casts an enum to its discriminant, then uses a numeric cast if needed.
373
+
374
+
#### Primitive to integer cast
375
+
376
+
*`false` casts to `0`, `true` casts to `1`
377
+
*`char` casts to the value of the code point, then uses a numeric cast if needed.
378
+
8` to `char` cast
379
+
* Casts to the `char` with the corresponding code point.
380
+
381
+
#### Pointer to address cast
382
+
383
+
Casting from a valid raw pointer to `usize` will produce the address that is pointed to.
384
+
385
+
The pointer's provenance is lost in this conversion.
386
+
387
+
#### Address to pointer cast
388
+
389
+
Casting from `usize` to a raw pointer will produce a raw pointer to the same location as the original pointer, if the `usize` was obtained through a pointer to address cast of a valid pointer.
390
+
391
+
<divclass="warning">
392
+
Warning:
393
+
The two pointers are not equivalent.
394
+
Dereferencing the pointer obtained from the address to pointer cast may be <ahref="../behavior-considered-undefined.md">undefined behavior</a> if aliasing rules are not followed.
0 commit comments