Skip to content

Commit a326d8d

Browse files
committed
Document non-obvious behavior of fmt::UpperHex & co for negative integers
Before stabilization I’d have suggested changing the behavior, but that time is past.
1 parent 71da1c2 commit a326d8d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/libcore/fmt/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,9 @@ pub trait Display {
624624
///
625625
/// The `Octal` trait should format its output as a number in base-8.
626626
///
627+
/// For primitive signed integers (`i8` to `i128`, and `isize`),
628+
/// negative values are formatted as the two’s complement representation.
629+
///
627630
/// The alternate flag, `#`, adds a `0o` in front of the output.
628631
///
629632
/// For more information on formatters, see [the module-level documentation][module].
@@ -639,6 +642,8 @@ pub trait Display {
639642
///
640643
/// assert_eq!(format!("{:o}", x), "52");
641644
/// assert_eq!(format!("{:#o}", x), "0o52");
645+
///
646+
/// assert_eq!(format!("{:o}", -16), "37777777760");
642647
/// ```
643648
///
644649
/// Implementing `Octal` on a type:
@@ -671,6 +676,9 @@ pub trait Octal {
671676
///
672677
/// The `Binary` trait should format its output as a number in binary.
673678
///
679+
/// For primitive signed integers (`i8` to `i128`, and `isize`),
680+
/// negative values are formatted as the two’s complement representation.
681+
///
674682
/// The alternate flag, `#`, adds a `0b` in front of the output.
675683
///
676684
/// For more information on formatters, see [the module-level documentation][module].
@@ -686,6 +694,8 @@ pub trait Octal {
686694
///
687695
/// assert_eq!(format!("{:b}", x), "101010");
688696
/// assert_eq!(format!("{:#b}", x), "0b101010");
697+
///
698+
/// assert_eq!(format!("{:b}", -16), "11111111111111111111111111110000");
689699
/// ```
690700
///
691701
/// Implementing `Binary` on a type:
@@ -719,6 +729,9 @@ pub trait Binary {
719729
/// The `LowerHex` trait should format its output as a number in hexadecimal, with `a` through `f`
720730
/// in lower case.
721731
///
732+
/// For primitive signed integers (`i8` to `i128`, and `isize`),
733+
/// negative values are formatted as the two’s complement representation.
734+
///
722735
/// The alternate flag, `#`, adds a `0x` in front of the output.
723736
///
724737
/// For more information on formatters, see [the module-level documentation][module].
@@ -734,6 +747,8 @@ pub trait Binary {
734747
///
735748
/// assert_eq!(format!("{:x}", x), "2a");
736749
/// assert_eq!(format!("{:#x}", x), "0x2a");
750+
///
751+
/// assert_eq!(format!("{:x}", -16), "fffffff0");
737752
/// ```
738753
///
739754
/// Implementing `LowerHex` on a type:
@@ -767,6 +782,9 @@ pub trait LowerHex {
767782
/// The `UpperHex` trait should format its output as a number in hexadecimal, with `A` through `F`
768783
/// in upper case.
769784
///
785+
/// For primitive signed integers (`i8` to `i128`, and `isize`),
786+
/// negative values are formatted as the two’s complement representation.
787+
///
770788
/// The alternate flag, `#`, adds a `0x` in front of the output.
771789
///
772790
/// For more information on formatters, see [the module-level documentation][module].
@@ -782,6 +800,8 @@ pub trait LowerHex {
782800
///
783801
/// assert_eq!(format!("{:X}", x), "2A");
784802
/// assert_eq!(format!("{:#X}", x), "0x2A");
803+
///
804+
/// assert_eq!(format!("{:X}", -16), "FFFFFFF0");
785805
/// ```
786806
///
787807
/// Implementing `UpperHex` on a type:

0 commit comments

Comments
 (0)