首页 > 解决方案 > 为什么使用带有 #![no_std] 的 rand/rand_core 会导致“重复的语言项”?

问题描述

我正在尝试使用randor生成随机数rand_core。根据文档#![no_std]可以使用default-features = false. 因此,我尝试为两者都这样做,randrand_core查看其中一个是否有效。在这两种情况下我都会遇到相同的错误,所以我展示了我为rand_core.

rand_core = {version = "0.5.1", default-features = false}

然后将其导入为:

use rand_core::RngCore;

但是,我收到以下错误(我没有编写这部分代码):

error[E0152]: found duplicate lang item `oom`
   --> src/alloc.rs:116:1
    |
116 | / fn alloc_error_handler(layout: Layout) -> ! {
117 | |     panic!("allocation error: {:?}", layout)
118 | | }
    | |_^
    |
    = note: the lang item is first defined in crate `std` (which `rand_core` depends on)

error[E0152]: found duplicate lang item `panic_impl`
  --> src/panic.rs:21:1
   |
21 | / fn panic(info: &PanicInfo) -> ! {
22 | |     let mut host_stderr = ErStderr::default();
23 | |
24 | |     writeln!(host_stderr, "{}", info).ok();
25 | |
26 | |     unsafe {libc::exit(1); }
27 | | }
   | |_^
   |
   = note: the lang item is first defined in crate `std` (which `rand_core` depends on)

error[E0152]: found duplicate lang item `eh_personality`
  --> src/panic.rs:29:28
   |
29 | #[lang = "eh_personality"] extern fn eh_personality() {}
   |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: the lang item is first defined in crate `panic_unwind` (which `std` depends on)

这是一个最小的工作示例:https ://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=7e748b901ea22742c878e8208d9c0691

我每晚使用。

这是我的Cargo.toml

[dependencies]
libc = "0.2"
cbindgen = "0.12.2"
rand_core = {version = "0.5.1", default-features=false}

[lib]
crate-type = ["staticlib"]

标签: rust

解决方案


我不知道为什么会这样,但是当我cbindgen从依赖项列表中删除时,一切都会自动运行。


推荐阅读