如何有条件地启用基于cfg的Rust功能?

时间:2020-06-10 09:50:37

标签: rust conditional-compilation

我想使用#![feature(custom_test_frameworks)],但如果可能,只能通过#[cfg(not(target_os = "custom_os_name"))]启用它。我仍然希望可以选择使用rusts libtest在主机系统上直接运行一些测试,但是显然不允许我通过cfg来修改功能:

错误消息:

note: inner attributes, like `#![no_std]`, annotate the item enclosing them, 
and are usually found at the beginning of source files. Outer attributes, like 
`#[test]`, annotate the item following them.

还有其他方法可以有条件地启用“内部属性”吗?

1 个答案:

答案 0 :(得分:4)

您可以使用#[cfg_attr]来基于其他功能应用属性。

您可以这样做:

#![cfg_attr(
    not(target_os = "custom_os_name"),
    feature(custom_test_frameworks)
)]