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

2

12

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?

Gond answered 2/9, 2016 at 11:55 Comment(0)
L
9

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.

Limn answered 2/9, 2016 at 11:59 Comment(6)
Thanks! I searched the Rust book but only looked around the explanations for the conditional compile. Thanks for pointing me to the right place!Gond
The 2nd part of my questions still stays: Is there any way to tell the compiler that such an attribute should cover more than one declaration?Gond
@Gond Just a quick tip about StackOverflow: please only ask one question per question-post. In this case it's kind of OK, since the questions are very closely linked. So just for your SO future: make sure to ask just one question; otherwise you risk downvotes. In that sense: welcome to SO :)Fluted
@LukasKalbertodt thanks for the hint and the warm welcome! I hope that my first question will not directly lead to a downvote :-)Gond
@Limn Thanks for extending your answer regarding the 2nd aspect!Gond
In more recent versions of Rust, it looks like you can use curly braces to cover multiple lines, for example: #[cfg(feature="blah")] { /* your code */ } Just watch the scope.Meridethmeridian
F
24

You can use a module to group together everything that should exist for debug/release only, like this:

#[cfg(debug)]
mod example {
    pub type A = i32;
    pub type B = i64;
}

#[cfg(not(debug))]
mod example {
    pub type A = u32;
    pub type B = u64;
}

fn main() {
    let x: example::A = example::A::max_value();
    println!("{}", x);
}

Playground link (note that this will always print the not(debug) value because the playground doesn't define the debug feature, even in debug mode).

If debug is defined, this will print 2147483647 (the maximum value of an i32), otherwise it will print 4294967295 (the maximum value of a u32). Keep in mind that both modules must have definitions for each item, otherwise you'll hit a compile-time error.

If you've not read about Attributes, it might be a good idea to do so; make sure you know the difference between inner attributes (#![attribute]) and outer attributes (#[attribute]).

Fraternity answered 2/9, 2016 at 12:40 Comment(2)
I did indeed consider to wrap my declarations with a module but finally decided against it. For me, a module is a way to structure my code and I don't want to Have a structure preset by the use of a conditional compile. I have to admit though, that I would solution if would have to do really a large amount of declarations (currently I only have to do 4)Gond
With only 4 declarations, it's probably manageable at the moment, although there's no real overhead from using a module and probably saves a few lines, especially if you need to scale it up in future. Is there any particular reason you're against using a module, or is it just too bulky for your needs?Fraternity
L
9

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.

Limn answered 2/9, 2016 at 11:59 Comment(6)
Thanks! I searched the Rust book but only looked around the explanations for the conditional compile. Thanks for pointing me to the right place!Gond
The 2nd part of my questions still stays: Is there any way to tell the compiler that such an attribute should cover more than one declaration?Gond
@Gond Just a quick tip about StackOverflow: please only ask one question per question-post. In this case it's kind of OK, since the questions are very closely linked. So just for your SO future: make sure to ask just one question; otherwise you risk downvotes. In that sense: welcome to SO :)Fluted
@LukasKalbertodt thanks for the hint and the warm welcome! I hope that my first question will not directly lead to a downvote :-)Gond
@Limn Thanks for extending your answer regarding the 2nd aspect!Gond
In more recent versions of Rust, it looks like you can use curly braces to cover multiple lines, for example: #[cfg(feature="blah")] { /* your code */ } Just watch the scope.Meridethmeridian

© 2022 - 2024 — McMap. All rights reserved.