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

jsc*_*chw 5 conditional-compilation rust

我想使用#![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.
Run Code Online (Sandbox Code Playgroud)

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

Pet*_*all 9

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

对于你的情况,你可以这样做:

#![cfg_attr(
    not(target_os = "custom_os_name"),
    feature(custom_test_frameworks)
)]
Run Code Online (Sandbox Code Playgroud)