为什么将 rand/rand_core 与 #![no_std] 结果为"duplicate lang item" ?



我正在尝试使用randrand_core生成随机数。根据文档,可以使用default-features = false启用#![no_std]使用。所以我试着对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=调试&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"]

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

最新更新