尝试访问紧急信息消息会导致类型不匹配错误?

问题描述 投票:0回答:1

尝试编译此 bootkit 项目时,出现错误:

error[E0308]: mismatched types
  --> bootkit\src/main.rs:33:16
   |
33 | if let Some(message) = info.message() {
   | ^^^^^^^^^^^^^ -------------- this expression has type `PanicMessage<'_>`
   | |
   | expected `PanicMessage<'_>`, found `Option<_>`.
   |
   = note: expected struct `PanicMessage<'_>`
                found enum `Option<_>`

这是发生错误的代码部分:

#[panic_handler]
fn panic_handler(info: &core::panic::PanicInfo) -> ! {
    if let Some(location) = info.location() {
        log::error!(
            "[-] Panic in {} at ({}, {}):",
            location.file(),
            location.line(),
            location.column()
        );
        if let Some(message) = info.message() {
            log::error!("[-] {}", message);
        }
    }

    loop {}
}
rust error-handling
1个回答
1
投票

以前返回的是

Option
,最近的
PanicInfo::message
,因为#115974直接返回了
PanicMessage
,只需删除
if let
即可。

#[panic_handler]
fn panic_handler(info: &core::panic::PanicInfo) -> ! {
    if let Some(location) = info.location() {
        log::error!(
            "[-] Panic in {} at ({}, {}):",
            location.file(),
            location.line(),
            location.column()
        );
        let message = info.message();
        log::error!("[-] {message}");
    }

    loop {}
}

不稳定的函数更改其签名的情况并不罕见,请务必检查文档的编译器版本以避免此类混淆。

© www.soinside.com 2019 - 2024. All rights reserved.