We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d07c43a commit a678b9aCopy full SHA for a678b9a
library/alloc/src/string.rs
@@ -2254,14 +2254,18 @@ static DEC_DIGITS_LUT: &[u8; 200] = b"0001020304050607080910111213141516171819\
2254
impl ToString for i8 {
2255
#[inline]
2256
fn to_string(&self) -> String {
2257
- let mut vec: Vec<u8> = if *self < 0 {
+ let mut n = *self;
2258
+ let mut vec: Vec<u8> = if n < 0 {
2259
+ // convert the negative num to positive by summing 1 to it's 2 complement
2260
+ // ( -128u8.abs() would panic )
2261
+ n = (!n).wrapping_add(1);
2262
let mut v = Vec::with_capacity(4);
2263
v.push(b'-');
2264
v
2265
} else {
2266
Vec::with_capacity(3)
2267
};
- let mut n = self.abs();
2268
+ let mut n = n as u8;
2269
if n >= 10 {
2270
if n >= 100 {
2271
n -= 100;
0 commit comments