Can you test if a feature of a dependency is set using the `cfg!` macro?

Can you test if a feature of a dependency is set using the cfg! macro? Something like:

if cfg!(feature = "some-dependency/some-feature") {
    println!("SET");
} else {
    println!("NOT SET");
}

Here is the question on stack overflow.

Yes.

Oh sorry, I didn't read the SO post which had more details. No it's not possible for dependencies.

Thanks, is there a fundamental reason that makes this functionality not possible? It seems odd that we can set them from the Cargo.toml files and the command line, but not read them with cfg.

I'm not at my pc so I can't test it, but if you run cargo build -vv it should show you the command line that cargo uses to call rustc. Since there's no --cfg argument for the dependency features, the compiler has no way of knowing. Sure, someone could add this feature to cargo, but this is how it is now.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.