diff --git a/ch2/user/src/bin/00hello_world.rs b/ch2/user/src/bin/00hello_world.rs index 77ef2fc..fbf31f5 100644 --- a/ch2/user/src/bin/00hello_world.rs +++ b/ch2/user/src/bin/00hello_world.rs @@ -1,6 +1,7 @@ #![no_std] #![no_main] +use user_lib; #[no_mangle] fn main() { diff --git a/ch2/user/src/lib.rs b/ch2/user/src/lib.rs index 04ead1b..7c21dcb 100644 --- a/ch2/user/src/lib.rs +++ b/ch2/user/src/lib.rs @@ -1,5 +1,8 @@ #![no_std] #![feature(linkage)] // 开启弱链接特性 +#![feature(panic_info_message)] + +mod user_lang_items; #[linkage = "weak"] // 设置我们默认的main函数, 弱链接 #[no_mangle] diff --git a/ch2/user/src/user_lang_items.rs b/ch2/user/src/user_lang_items.rs new file mode 100644 index 0000000..59f155e --- /dev/null +++ b/ch2/user/src/user_lang_items.rs @@ -0,0 +1 @@ +pub mod user_panic; diff --git a/ch2/user/src/user_lang_items/user_panic.rs b/ch2/user/src/user_lang_items/user_panic.rs new file mode 100644 index 0000000..0fd119e --- /dev/null +++ b/ch2/user/src/user_lang_items/user_panic.rs @@ -0,0 +1,18 @@ +use core::panic::PanicInfo; + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + if let Some(location) = info.location() { + // println!( + // "Panicked at {}:{} {}", + // location.file(), + // location.line(), + // info.message().unwrap() + // ); + } else { + // println!("Panicked: {}", info.message().unwrap()); + } + loop { + + } +} \ No newline at end of file