Skip to content

Commit bb6f41b

Browse files
committed
Avoid the use of overflowing_literals in the examples
I think that it could have some potential confusion since it isn't displayed in the text that these are overflowing, and it requires an extra layer of reasoning needed to understand how it translates.
1 parent 4226cec commit bb6f41b

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

src/expressions/operator-expr.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -400,39 +400,35 @@ reference types and `mut` or `const` in pointer types.
400400
(Rust uses 2's complement for negative values of fixed integers)
401401

402402
```rust
403-
# #![allow(overflowing_literals)]
404403
assert_eq!(42i8 as u8, 42u8);
405404
assert_eq!(-1i8 as u8, 255u8);
406405
assert_eq!(255u8 as i8, -1i8);
407-
assert_eq!(0xffu8 as i8, 0xffi8);
408406
assert_eq!(-1i16 as u16, 65535u16);
409407
```
410408

411409
* Casting from a larger integer to a smaller integer (e.g. u32 -> u8) will
412410
truncate
413411

414412
```rust
415-
# #![allow(overflowing_literals)]
416413
assert_eq!(42u16 as u8, 42u8);
417414
assert_eq!(1234u16 as u8, 210u8);
418415
assert_eq!(0xabcdu16 as u8, 0xcdu8);
419416

420417
assert_eq!(-42i16 as i8, -42i8);
421418
assert_eq!(1234u16 as i8, -46i8);
422-
assert_eq!(0xabcdi16 as i8, 0xcdi8);
419+
assert_eq!(0xabcdi32 as i8, -51i8);
423420
```
424421

425422
* Casting from a smaller integer to a larger integer (e.g. u8 -> u32) will
426423
* zero-extend if the source is unsigned
427424
* sign-extend if the source is signed
428425

429426
```rust
430-
# #![allow(overflowing_literals)]
431427
assert_eq!(42i8 as i16, 42i16);
432428
assert_eq!(-17i8 as i16, -17i16);
433429
assert_eq!(0b1000_1010u8 as u16, 0b0000_0000_1000_1010u16, "Zero-extend");
434430
assert_eq!(0b0000_1010i8 as i16, 0b0000_0000_0000_1010i16, "Sign-extend 0");
435-
assert_eq!(0b1000_1010i8 as i16, 0b1111_1111_1000_1010i16, "Sign-extend 1");
431+
assert_eq!(0b1000_1010u8 as i8 as i16, 0b1111_1111_1000_1010u16 as i16, "Sign-extend 1");
436432
```
437433

438434
* Casting from a float to an integer will round the float towards zero

0 commit comments

Comments
 (0)