问题
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