Custom panic handler recognized as a macro

So I have the next code:

std::panic::set_hook(Box::new(|panic_info| {
    if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
        log!("panic occurred: {:?}", s);
    } else {
        log!("panic occurred");

But if I attempt to compile it, I get:

error: expected one of `!` or `::`, found `(`
  --> src/
41 | std::panic::set_hook(Box::new(|panic_info| {
   |                     ^ expected one of `!` or `::`

Someone has an idea?

You have to put the call to std::panic::set_hook inside a function, probably your main function.

Thanks, The code in the documentation made me think that I can define it globally.

Thank you.

Yeah, it does kind of look that way. But any code in a doctest on std - Rust or should be thought of as being inside fn main() { ... }—that's how cargo test --doc compiles and runs them.

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.