How do I test crates with #![no_std]?

会有一股神秘感。 提交于 2020-07-08 06:25:33

问题


I'm writing a runtime for a programming language implementation in Rust. I'm planning on linking in this runtime with the compiled code I generate, so to keep the binary small I don't want to rely on std.

When I try to cargo test my runtime, I get errors saying saying that std::slice::AsSlice can't be found, which I found is because some of the test harness requires std library code.

How do I go about testing this code? Is there a way to conditionally include the #![no_std] pragma, i.e. still include the std library while testing? I've also tried creating a separate test crate with the std library included, extern crateing the runtime crate into it and running my tests there, but that has introduced a whole new set of issues.


回答1:


#[cfg(test)]
#[macro_use]
extern crate std;

The #[macro_use] part is optional in Rust 2015 and not required in Rust 2018.



来源:https://stackoverflow.com/questions/28185854/how-do-i-test-crates-with-no-std

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!