From 6b66749e171e8a9c6718c0107f4ec2e3f47b28ed Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Wed, 20 Jan 2021 01:33:38 +0000 Subject: [PATCH 1/2] Use slice::split_first instead of manuall slicing --- library/std/src/net/ip.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index d33b772633d..5c62ce43afb 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -1610,9 +1610,9 @@ impl fmt::Display for Ipv6Addr { /// Write a colon-separated part of the address #[inline] fn fmt_subslice(f: &mut fmt::Formatter<'_>, chunk: &[u16]) -> fmt::Result { - if let Some(first) = chunk.first() { + if let Some((first, tail)) = chunk.split_first() { fmt::LowerHex::fmt(first, f)?; - for segment in &chunk[1..] { + for segment in tail { f.write_char(':')?; fmt::LowerHex::fmt(segment, f)?; } From 116b66ad491b3f0a5a809e49a6377d2697d0ed1b Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Wed, 20 Jan 2021 03:06:49 +0000 Subject: [PATCH 2/2] Dont prefix 0x when `dbg!(ipv6)` --- library/std/src/net/ip.rs | 4 ++-- library/std/src/net/ip/tests.rs | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index 5c62ce43afb..84449e48767 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -1611,10 +1611,10 @@ impl fmt::Display for Ipv6Addr { #[inline] fn fmt_subslice(f: &mut fmt::Formatter<'_>, chunk: &[u16]) -> fmt::Result { if let Some((first, tail)) = chunk.split_first() { - fmt::LowerHex::fmt(first, f)?; + write!(f, "{:x}", first)?; for segment in tail { f.write_char(':')?; - fmt::LowerHex::fmt(segment, f)?; + write!(f, "{:x}", segment)?; } } Ok(()) diff --git a/library/std/src/net/ip/tests.rs b/library/std/src/net/ip/tests.rs index 44fb3adf070..ef0d4edc434 100644 --- a/library/std/src/net/ip/tests.rs +++ b/library/std/src/net/ip/tests.rs @@ -166,6 +166,9 @@ fn ipv6_addr_to_string() { // two runs of zeros, equal length assert_eq!("1::4:5:0:0:8", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8).to_string()); + + // don't prefix `0x` to each segment in `dbg!`. + assert_eq!("1::4:5:0:0:8", &format!("{:#?}", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8))); } #[test]