【问题标题】:How can I conditionally enable a Rust feature based on cfg?如何有条件地启用基于 cfg 的 Rust 功能?
【发布时间】:2020-09-29 16:41:07
【问题描述】:

我想使用#![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.

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

【问题讨论】:

    标签: rust conditional-compilation


    【解决方案1】:

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

    在你的情况下,你可以这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-08
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多