如何使用 #![no_std] 测试板条箱? [英] How do I test crates with #![no_std]?

查看:20
本文介绍了如何使用 #![no_std] 测试板条箱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Rust 中的编程语言实现编写运行时.我计划在这个运行时链接我生成的编译代码,所以为了保持二进制小我不想依赖 std.

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.

当我尝试cargo test我的运行时,我收到错误说找不到std::slice::AsSlice,我发现这是因为一些测试工具需要标准库代码.

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.

我该如何测试这段代码?有没有办法有条件地包含 #![no_std] 编译指示,即在测试时仍然包含 std 库?我还尝试创建一个包含 std 库的单独测试包,extern crate将运行时包放入其中并在那里运行我的测试,但这引入了一系列新问题.>

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.

推荐答案

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

#[macro_use] 部分在 Rust 2015 中是可选的,在 Rust 2018 中不需要.

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

这篇关于如何使用 #![no_std] 测试板条箱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆