Search code examples
rustconditional-compilation

How many lines are covered by the Rust conditional compilation attribute?


I'm trying to use a conditional compilation statement. Beyond defining a function that should only exist in a debug build, I want to define a set of variables/constants/types that only exist in the debug build.

#[cfg(debug)]
pub type A = B;
pub type B = W;

#[cfg(other_option)]
pub type A = Z;
pub type B = I;
let test = 23i32;

How many lines are actually "covered" by the conditional compile attribute in this case? Is it only one (what I would expect in this context)? Are there ways to ensure that a whole block of code (including variables, types and two functions) is covered by the condition?


Solution

  • An #[attribute] only applies to the next item. Please see the Rust book.

    Edit: I don't think it is currently possible to spread an attribute over an arbitrary number of declarations.

    Additional, in-depth information on attributes and their application can be found at Rust reference.