How to get rid of panic handler?

#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop{}
}

#[no_mangle]
pub extern "C" fn _start() -> ! {
    let  mut x: i32 = 3;
    loop{
        x = x + 1;
    }
}

Hi all,
As I start with RUST on risc-v, I am able to compile above and run in my target. Can someone help me on how to get rid of the panic handler? I am new to rust & it seems RUST enforces a panic handler I may be wrong. so in RUST is panic hanlder mandatory? if not, what changes would help me to get rid of the panic handler?

It is indeed required, because certain things (e.g. indexing) can trigger panics even if it is not explicit.

1 Like

In your specific example the x + 1 will panic once it overflows if debug assertions are enabled.

2 Likes

Thanks, but i want to build my code WITHOUT any library. Hence, is there a way to get away this panic handler inturn get away with the core library? Is it possible? or its mandatory to have panic handler in RUST?

It is mandatory to have some panic handler. The handler can crash, abort the program, or loop forever, but it must exist.

In this case you can use #![no_core] in addition to #![no_std]. Not sure if this is what you want, however, since in this case you'll have to provide literally every reference to the used compiler intrinsics.

It's mandatory even for no_core, yes, since there are some operations which has the implicit panic semantics.

1 Like

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.