Impl `Debug` for `panic!` macro

I often find myself writing like the following when debugging:

let interesting_variable = 123;
dbg!(&interesting_variable);
panic!();

I add the panic macro to improve signal/noise ratio since there's a whole lotta other logs spamming the CLI output, and panic allows me to focus the CLI output on specifically the interesting_variable that I'm trying to inspect.

Would be nice if support for Debug trait is added to panic!() macro so I can write like this instead:

let interesting_variable = 123;
panic!(interesting_variable);
panic!("{:?}", interesting_variable);
5 Likes

this seems more verbose than necessary

panic!(x) was removed in Edition 2021. See Panic macro consistency - The Rust Edition Guide

It's not coming back.

4 Likes

Or the alternate spelling:

panic!("{interesting_variable:?}");
4 Likes

Just write your own macro if you need this often, it takes a couple of lines of code and as a bonus you can make the debug message meaningful and won’t be abusing panic! for something that is not a panic.

5 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.