# Cfg, keyword for "if multiple/more than one"

**URL:** <https://internals.rust-lang.org/t/cfg-keyword-for-if-multiple-more-than-one/15751>\
**Category:** Uncategorized\
**Created:** [December 8, 2021, 10:24pm UTC](https://internals.rust-lang.org/t/cfg-keyword-for-if-multiple-more-than-one/15751 "2021-12-08T22:24:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arisa](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arisa/32/8816_2.png) [@Arisa](https://internals.rust-lang.org/u/Arisa)\
**Post date:** [December 8, 2021, 10:24pm UTC](https://internals.rust-lang.org/t/cfg-keyword-for-if-multiple-more-than-one/15751/1 "2021-12-08T22:24:39Z")

</div>

Hi, GitHub sent me here when I tried to create a feature request, if this is the wrong place to post this in I apologize

I am at the moment working on a program that has a features for multiple Linux distro's example.:

> arch, manjaro, garuda, debian

To compile the program it always needs one feature enabled, but now I wanted to handle a case in which someone tries to compile it with more features but I can't seem to create a cfg which will allow me to basically do "if more than one -\> allow compilation", I wanted to make a custom compile error if someone provides more of them so it's more clear, because when I don't do something like this the compiler just returns an error about duplicate struct and such which is not best for a just maintainer of the package and not a developer.

I think having some keyword like "multiple" or something like that to be able to do this would be useful.

```rust
#[cfg(multiple(feature = "manjaro", feature = "arch", feature = "garuda", feature = "debian"))]
compile_error!("You can't compile this program to support multiple distros at the same time! It must be compiled separately for each one!");

```

---

<div class="post-metadata">

**Author:** ![ekuber](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ekuber/32/1646_2.png) [@ekuber](https://internals.rust-lang.org/u/ekuber)\
**Post date:** [December 8, 2021, 10:43pm UTC](https://internals.rust-lang.org/t/cfg-keyword-for-if-multiple-more-than-one/15751/2 "2021-12-08T22:43:52Z")

</div>

An option you have is to have a sentinel `const` for each of your feature gates: if multiple features are enabled, then you'd get a duplicated `const` error. The error message won't be as good as what you desire, but it will do the trick to preclude compilation.

---

<div class="post-metadata">

**Author:** ![Arisa](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arisa/32/8816_2.png) [@Arisa](https://internals.rust-lang.org/u/Arisa)\
**Post date:** [December 8, 2021, 10:48pm UTC](https://internals.rust-lang.org/t/cfg-keyword-for-if-multiple-more-than-one/15751/3 "2021-12-08T22:48:08Z")

</div>

That's clever, Thank you, I will probably use that for now but this keyword for it would be very nice instead of this trick

---

<div class="post-metadata">

**Author:** ![matt1985](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matt1985/32/4772_2.png) [@matt1985](https://internals.rust-lang.org/u/matt1985)\
**Post date:** [December 9, 2021, 1:25am UTC](https://internals.rust-lang.org/t/cfg-keyword-for-if-multiple-more-than-one/15751/4 "2021-12-09T01:25:27Z")

</div>

In the current stable Rust version (1.57.0), you can use a static assertion to get a `compile_error`-like error message: [https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=825e435532bd68f2b2d82ca8966e59e0](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=825e435532bd68f2b2d82ca8966e59e0)

```rust
const _: () = {
    let enabled_features = {
        cfg!(feature = "manjaro") as u32 +
        cfg!(feature = "arch") as u32 +
        cfg!(feature = "garuda") as u32 + 
        cfg!(feature = "debian") as u32
    };

    match enabled_features {
        0 => panic!("\nNone of the distro features were enabled, exactly one must be."),
        1 => {}
        2.. => panic!("\nYou can't compile this program to support multiple distros at the same time! It must be compiled separately for each one!"),
    }
};

```

```rust
error[E0080]: evaluation of constant value failed
  --> src/lib.rs:10:14
   |
10 | 0 => panic!("\nNone of the distro features were enabled, exactly one must be."),
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at '
None of the distro features were enabled, exactly one must be.', src/lib.rs:10:14
   |
   = note: this error originates in the macro `$crate::panic::panic_2021` (in Nightly builds, run with -Z macro-backtrace for more info)

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 9, 2022, 1:26am UTC](https://internals.rust-lang.org/t/cfg-keyword-for-if-multiple-more-than-one/15751/5 "2022-03-09T01:26:29Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
