Conditional compilation of compiler attributes?


Is it possible to use conditional compilation to include or exclude compiler attributes? For example, something like:

#[cfg(feature = "no-inline")]
fn foo() {}

(in my case, I’m trying to turn off inlining so I can get more granular instrumentation of my code)


Yes, you should be able to use cfg_attr:

#![cfg_attr(feature = "no-inline", inline(never))]
fn foo() {}


Awesome, thanks! Also, it’s #[cfg_attr()], not #![cfg_attr()] (or at least, the latter worked for me while the former gave me errors: “an inner attribute is not permitted in this context”).


Ah, yes, one applies to the whole file/program and one applies to a single item. I copied a global one and forgot to change it.


Ah, gotcha.