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

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

    .unwrap_or_else(|e| panic!(e));

This compiles, and produces:

thread 'main' panicked at 'Box<Any>', src/

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

    .unwrap_or_else(|e| panic!("{}", e));
thread 'main' panicked at 'invalid float literal', src/

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)...


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