0

I'm trying to understand the following example from the condition compilation manual @ doc.rust-lang.org:

// This function is only included when either foo or bar is defined
#[cfg(any(foo, bar))]
fn needs_foo_or_bar() {
  // ...
}

What do those foo and bar identifiers represent?

Is this a shortcut for target_os identifiers or what is it for?

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
  • Unfortunately you have to scroll down a bit to actually find some usable examples of what can take the places of `foo` and `bar`: https://doc.rust-lang.org/reference/conditional-compilation.html#forms-of-conditional-compilation – BallpointBen Sep 20 '22 at 05:05

2 Answers2

1

Turns out it had nothing to do with target_os, it is what you set in RUSTFLAGS when you build/run:

RUSTFLAGS='--cfg foo'

Which configuration options are set is determined statically during the compilation of the crate. Certain options are compiler-set based on data about the compilation. Other options are arbitrarily-set, set based on input passed to the compiler outside of the code.

Related question with a bit more advanced example

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
0

From the syntax definition on the conditional compilation manual, any(foo, bar) is a ConfigurationPredicate (specifically the ConfigurationAny variation) so foo, bar is a ConfigurationPredicateList and thus foo and bar are each a ConfigurationPredicate. So you could use them to conditionally compile for a target OS. Or you could do some custom features like this:

#[cfg(any(feature = "myfeature1", feature = "myfeature2"))]
pub struct Source {
    pub email: String,
    pub name: String,
}

For more on custom features, see this question

Andrew
  • 904
  • 5
  • 17