-1

In my .toml file I define a feature turned off by default:

[features]
foo = []

If this feature is enabled I want to print "FOO":

fn main() {
    #[cfg(feature = "foo")]
    println!("FOO");
}

I can then compile (and run) the code like this: cargo run --features foo

However, I'd prefer to use the shorthand that I see in the docs. Something like this:

fn main() {
    #[cfg(foo)]
    println!("FOO");
}

However, when using the same cargo run command as earlier the print statement doesn't compile. What am I missing?

HiDefender
  • 2,088
  • 2
  • 14
  • 31
  • Well, that's just not what the syntax for querying attributes is. You could implement a proc macro so you can write `#[foo]` instead of `#[cfg(feature = "foo")]` if you really want to, but in my opinion simply using the syntax everyone else uses make the code much easier to understand. – Sven Marnach Aug 21 '20 at 18:43
  • 1
    @SvenMarnach In the link given above, what is meant in the docs when they give the example: `#[cfg(not(foo))]`. What is `foo` in this circumstance? – HiDefender Aug 21 '20 at 18:48
  • @HiDefender `// This function is only included when foo is not defined`. Turning on feature "foo" is adding the *value* `foo` to `feature`, making *feature* defined. – Herohtar Aug 21 '20 at 20:05

1 Answers1

0

According to the docs.

#[cfg(foo)] is a configuration option name. And can be set as so: rustc --cfg foo main.rs. For how to set it with cargo see this question.

#[cfg(feature = "foo")] is a configuration option key-value pair.

HiDefender
  • 2,088
  • 2
  • 14
  • 31