Skip to content

panic_handler isn't just for no_std applications #373

@grahamc

Description

@grahamc

The documentation on https://doc.rust-lang.org/nomicon/panic-handler.html suggests that specifying panic handlers is just for no_std applications:

#[panic_handler] is used to define the behavior of panic! in #![no_std] applications.

However it may also be used for other applications too, like this Play link:

https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Apanic%3B%0A%0Apanic%3A%3Aset_hook(Box%3A%3Anew(%7Cpanic_info%7C%20%7B%0A%20%20%20%20if%20let%20Some(s)%20%3D%20panic_info.payload().downcast_ref%3A%3A%3C%26str%3E()%20%7B%0A%20%20%20%20%20%20%20%20println!(%22panic%20occurred%3A%20%7Bs%3A%3F%7D%22)%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20println!(%22panic%20occurred%22)%3B%0A%20%20%20%20%7D%0A%7D))%3B%0A%0Apanic!(%22Normal%20panic%22)%3B%0A%7D&edition=2021

This was quite confusing for me because the nomicon was the first resource in DDG for "rust panic handler", which suggested that indeed I was wading into spooky waters:

image

I'm not sure the best way to rephrase it, but I think it shouldn't suggest that it is only for no_std.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions