Unintuitive way for creating own panic_handler

Hello! I have the following code (assuming we're in the src directory):

main.rs

#![no_std]
#![no_main]
#![feature(const_ptr_offset)]
#![feature(custom_test_frameworks)]
#![test_runner(my_runner)]

#[no_mangle]
pub extern "C" fn _entry() -> ! {
    loop {}
}
fn my_runner(tests: &[&i32]) {
    loop{}
}

lib.rs

#![no_std]
#![no_main]
#![feature(const_ptr_offset)]

use core::panic::PanicInfo;

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

target.json

{
    "llvm-target": "x86_64-unknown-none",
    "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
    "arch": "x86_64",
    "target-endian": "little",
    "target-pointer-width": "64",
    "target-c-int-width": "32",
    "os": "none",
    "executables": true,
    "linker-flavor": "ld.lld",
    "linker": "rust-lld",
    "panic-strategy": "abort",
    "disable-redzone": true,
    "features": "-mmx,-sse,+soft-float"
}

.cargo/config.toml

[build]
target = "target.json"

[unstable]
build-std = ["core", "compiler_builtins"]
build-std-features = ["compiler-builtins-mem"]

(I've set up rustup to use nightly) Now if I do:

cargo clean && cargo build -Zbuild-std

then I'm getting the following error message:

error: `#[panic_handler]` function required, but not found

error: could not compile `project` due to previous error

Which was confusing for me since I implemented it in lib.rs. I got the answer on the unofficial rust-discord server that I needed to add the following in main.rs:

use project

which worked. Why is it needed to add the use <crate> in the main function in order to compile the custom panic handler as well? For me, it was pretty unintuitive to just add use project in order to compile the custom panic handler.

Without the use project; the project crate is not referenced in any way and thus not linked in. Referencing any item inside the project crate would work just as well.

ok, thx.

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.