Rust 错误 -- `internal::Local` 未初始化,这是无效的

Rust Error -- `internal::Local` uninitialized, which is invalid

我正在尝试使用 Rust 中的通道将枚举类型数据传递给线程,但是当我 运行 程序(编译正常)时,我不断收到以下错误,

thread 'thread 'main' panicked at '' panicked at 'attempted to leave type internal::Local uninitialized, which is invalid. Attempted to leave type internal::Local uninitialized, which is invalid'

这是代码片段,

use std::thread;
use crossbeam_channel::unbounded;

enum Message {
    Sum(i64, i64),
    Quit
}

fn main() {

    let (sender, receiver) = unbounded();
    
    let worker = thread::spawn(move || loop {
            match receiver.recv() {
                Some(msg) => match msg  {
                    Message::Sum(a, b) => println!("{} + {} = {}", a, b, a+b),
                    Message::Quit => {
                         println!("Thread Terminating");
                         break
                    },
                },
                None => {
                    print!("Didn't receive or unable to read the message");
                    break;
                }
            }
        }
    );

    sender.send(Message::Sum(10, 21));
    sender.send(Message::Quit);

    worker.join();
}

这里是 Cargo.toml 的相关部分:

[dependencies]
crossbeam-channel = "0.2.5"

简而言之:此错误表明旧 crossbeam 版本的不健全,您必须更新它才能使您的代码正常工作。


如果我们 运行 来自 OP 的代码 RUST_BACKTRACE=1,我们将看到以下内容:

thread '<unnamed>' panicked at 'attempted to leave type `internal::Local` uninitialized, which is invalid', /home/cerberus/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-epoch-0.6.1/src/internal.rs:485:59
// ...skipped
   3: core::mem::uninitialized
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/mem/mod.rs:676:9
   4: <crossbeam_epoch::internal::Local as crossbeam_epoch::sync::list::IsElement<crossbeam_epoch::internal::Local>>::entry_of
             at /home/cerberus/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-epoch-0.6.1/src/internal.rs:485:59
// ...skipped

因此,此代码在内部使用 core::mem::uninitialized. And here's the exact source of panic - 内部函数,如果传递类型的未初始化值为 UB,则会发生恐慌。