How to detect if an unstable feature is enabled?

Viewed 213

I'm attempting to detect if an unstable feature (e.g., backtrace) is enabled in Rust 1.52. It seems like, cfg!(feature = "backtrace") should do it, but it doesn't. See this example:

#![feature(backtrace)]

fn main() {
    assert!(
        dbg!(cfg!(feature = "backtrace")) || dbg!(cfg!(backtrace)),
        "OK, then. How do you detect if an unstable feature is enabled?"
    );
}

The output is:

[src/main.rs:5] cfg!(feature = "backtrace") = false
[src/main.rs:5] cfg!(backtrace) = false
thread 'main' panicked at 'OK, then. How do you detect if an unstable feature is enabled?', src/main.rs:4:5

Playground.

Is there a way to detect if an unustable feature has been enabled?


For a little more background, I'm doing this in a proc macro, so I'm trying to use cfg!() to generate the correct code based on whether certain unstable features are enabled. I could use the #[cfg()] attribute, if that somehow worked while the cfg! macro didn't. But I get the same results with the #[cfg()] attribute.

0 Answers
Related