Why does core::panic require &str?

In a no_std crate, when using core::panic it will only accept &str. The documentation claims you can do panic!(4) but this gives an error. Is the documentation wrong or am I doing something wrong?

See Playground

It looks like calling panic! with a non-&str first argument involves creating a Box<dyn Any>, which requires dynamic memory allocation. With #![no_std] + extern crate alloc; you get a different set of errors: playground.

I think the documentation is not very clear on this point, and anyway it uses the deprecated dyn-less syntax for trait objects, so maybe a PR is in order.

1 Like

Ah so it's a documentation issue. I guess it just copied the std docs.

It is a shame it's impossible to pass anything other than a &str unless I use std but I understand why.

Ah it seems plans are underway to merge the two panics: Tracking Issue for RFC 3007: Making core and std's panic identical in Rust 2021 · Issue #80162 · rust-lang/rust · GitHub


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.