Example of how to use Conditional Compilation Macros in Rust

前端 未结 1 1486
太阳男子
太阳男子 2020-11-30 14:25

I\'ve followed quite a bit of the documentation and tried to reuse an example, but I can\'t get my code to work.

My Cargo.toml looks like this:

[pac         


        
相关标签:
1条回答
  • 2020-11-30 15:01

    The correct way to test for a feature is feature = "name", as you can see in the documentation you linked if you scroll a bit:

    As for how to enable or disable these switches, if you’re using Cargo, they get set in the [features] section of your Cargo.toml:

    [features]
    # no features by default
    default = []
    
    # Add feature "foo" here, then you can use it. 
    # Our "foo" feature depends on nothing else.
    foo = []
    

    When you do this, Cargo passes along a flag to rustc:

    --cfg feature="${feature_name}"
    

    The sum of these cfg flags will determine which ones get activated, and therefore, which code gets compiled. Let’s take this code:

    #[cfg(feature = "foo")]
    mod foo {
    }
    

    In your case using the cfg! macro, this would map to cfg!(feature = "foo").

    0 讨论(0)
提交回复
热议问题