Why does dbg! of Ipv6Addr show leading '0x'?

In the following code snippet:

let ip = Ipv6Addr::new(123, 0, 3, 0, 0, 0, 36, 2);
dbg!(&ip);
eprintln!("{:?}", &ip);

dbg! shows 0x7b:0x0:0x3::0x24:0x2 while eprintln! shows 7b:0:3::24:2. What is the reason behind this difference? Is this a bug?

playground

github issue

dbg! pretty-prints the value, so it's essentially equivalent to eprintln!("{:#?}", &ip) - note the hash symbol.

1 Like

To expand on that, it does seem to be a consequence of the pretty printer.

If you look at the display implementation for Ipv6Addr you'll see it uses std::fmt::LowerHex to write out the numbers. The docs for LowerHex say that:

The alternate flag, # , adds a 0x in front of the output.

So because dbg! uses #? all the hex numbers get a 0x put in front.

I think this is probably an unintended effect, especially as it's inconsistent with the special case versions (e.g. ::1). If you want you can try submitting an issue on the Rust repo and see if people agree that it's a bug.

6 Likes

For what it's worth I agree this is not intended, and is almost certainly an oversight.