Does anyone use panic!(expr) (i.e. panicked at Box<Any>)

And I mean, use it on purpose. For those not in the know:

"a".parse::<f64>()
    .unwrap_or_else(|e| panic!(e));

This compiles, and produces:

thread 'main' panicked at 'Box<Any>', src/main.rs:2:43

Whoops! What a terrible error message. Of course, what I obviously meant to write was:

"a".parse::<f64>()
    .unwrap_or_else(|e| panic!("{}", e));
thread 'main' panicked at 'invalid float literal', src/main.rs:2:43

Why does the first option even exist? It seems like a huge footgun to me.

It’s something of an artifact from old-Rust which was more of a CSP-style language.

I do use it, though, to be able to add more bits of context to a panic that’s used by a custom hook for better logging.

1 Like

Okay, so a custom handler set through std::panic::set_hook can downcast the payload.

It feels to me like this extremely niche functionality should not have been spelt as panic!($expr), but rather as panic_with_payload!($expr) or panic!(payload=$expr)

3 Likes