In Rust 1.95 and later, what does `cfg_select!` give you over a stack of `#[cfg]` items with a `not(any(...))` fallback?
answer
- a match over predicates
- first true arm wins
- underscore as fallback
- error when nothing matches
basics
~20 scfg_select!, stable since Rust 1.95, tries its predicates in order and expands only the first true arm, with _ as a fallback and a compile error if none match. You stop repeating negated predicates, and overlapping ones no longer collide.
solid answer
~40 s`cfg_select!` works like a `match` over cfg predicates. Arms are tried in source order, only the first true one is emitted, `_` always matches, and if no arm matches rustc stops with "none of the predicates in this `cfg_select` evaluated to true". With plain `#[cfg]` items every branch needs a predicate that is false whenever another branch is true, so the fallback becomes `not(any(...))` of everything above it, and an overlap such as `unix` plus `target_os = "macos"` defines the item twice (E0428). `cfg_select!` works in item and expression position, needs no import, and the arms it does not pick are not emitted, exactly like `#[cfg]`. It was stabilised in 1.95; a crate whose minimum supported Rust version is older keeps `#[cfg]` pairs or the `cfg-if` crate.
code
rust · 22 linescfg_select! {
target_os = "linux" => {
fn backend() -> &'static str { "inotify" }
}
target_os = "macos" => {
fn backend() -> &'static str { "FSEvents" }
}
windows => {
fn backend() -> &'static str { "ReadDirectoryChangesW" }
}
_ => {
fn backend() -> &'static str { "polling" }
}
}
fn main() {
let width = cfg_select! {
target_pointer_width = "64" => "64-bit",
_ => "not 64-bit",
};
println!("{} on a {width} target", backend());
}go deeper
Recall that cfg_select! picks one block by platform, with _ as the default arm.
Explain first-match order, the error when nothing matches, expression versus item position, and why it removes the not(any(...)) and E0428 bookkeeping.
Weigh adoption against the crate's minimum supported Rust version, since 1.95 is required, and watch the 1.99 lint for arms placed after _.
Decide whether a codebase migrates from the cfg-if crate or #[cfg] pairs to cfg_select!, balancing the MSRV bump against clearer platform selection.
## The problem `cfg_select!` solves Before Rust 1.95, picking one of several platform implementations meant writing one item per alternative, each with its own `#[cfg]`. Because rustc removes every item whose predicate is false and keeps every item whose predicate is true, the predicates had to be **mutually exclusive and exhaustive** by hand: - the fallback needed `#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]`, repeating every earlier predicate negated; - adding a platform meant editing the fallback as well, and forgetting to left a gap that surfaced as **E0425** at the call sites; - two predicates that overlap — `unix` and `target_os = "macos"` are both true on a Mac — kept both items and failed with **E0428**, "the name ... is defined multiple times". Many crates used the `cfg-if` crate's `cfg_if!` macro to get if / else-if chains instead. The standard library's documentation for `cfg_select!` lists `cfg_if` and `cfg-if` as search aliases, which shows the lineage. ## How `cfg_select!` works `cfg_select!` was **stabilised in Rust 1.95** and is a built-in macro that needs no import. It reads like a `match` over predicates: 1. Arms are written `predicate => payload` and are tried **in source order**. 2. The macro **expands to the payload of the first arm whose predicate is true**; later arms are ignored even if they are true too. 3. **`_` always evaluates to true**, so it is the fallback arm. 4. If **no** predicate is true, compilation fails with "none of the predicates in this `cfg_select` evaluated to true". In practice rustc then also reports E0425 at each use of an item that was never defined. 5. If an arm's whole payload is wrapped in braces, the braces are removed during expansion, so an arm can hold several items. It works in two positions: - **item position**, where each arm defines functions, modules, `use` declarations or `impl` blocks; - **expression position**, where each arm is a value: `let width = cfg_select! { target_pointer_width = "64" => "64-bit", _ => "not 64-bit" };`. Expression position matters because `#[cfg]` cannot be attached to a bare expression (E0658), and `cfg!` would type-check every branch. ## What is checked in the arms that are not chosen `cfg_select!` behaves like `#[cfg]`, not like `cfg!`, for the arms it does not pick: they are **not emitted**, so they are never name-resolved or type-checked on that target. An arm calling a Windows-only function builds fine on Linux. Each arm must still be a syntactically valid expansion for the position where the macro is invoked. The consequence is the same as with `#[cfg]`: you only learn that an unselected arm still compiles by building its target. ## Comparison | Concern | `#[cfg]` items | `cfg!` | `cfg_select!` | |---|---|---|---| | Removes unselected code | yes | no, both branches kept | yes | | Overlapping predicates | duplicate definitions, E0428 | not applicable | first true arm wins | | Fallback | hand-written `not(any(...))` | `else` branch | `_` arm | | No alternative matches | E0425 at the call sites | not possible | the macro's own compile error | | Works on a bare expression | no, E0658 | yes | yes | | Available since | Rust 1.0 | Rust 1.0 | Rust 1.95 | ## Lints and version constraints - rustc 1.99 warns about an arm written after `_` with **"unreachable configuration predicate"**, from the `unreachable_cfg_select_predicates` lint, which 1.99 added to the `unused` group. An arm after `_` can never be chosen, so it is almost always an ordering mistake. - The predicates are checked by the **`unexpected_cfgs`** lint like any other cfg, so a misspelled `target_os` value warns under Cargo. - A crate whose **minimum supported Rust version** is below 1.95 cannot use the macro; it keeps `#[cfg]` pairs or the `cfg-if` crate until it raises that minimum. Because the macro is a built-in rather than an edition change, it is not tied to the 2024 edition: what matters is a compiler of 1.95 or newer. ## A pattern worth recognising The standard library itself now selects platform modules this way. Its file-system layer has a `cfg_select!` whose first arm, `any(target_family = "unix", target_os = "wasi")`, declares `mod unix; use unix as imp;`, a `target_os = "windows"` arm doing the same with `windows`, and further arms for other systems; everything else in that layer talks only to `imp`. That is the shape a library with several platform back ends wants: one place that chooses, first match wins, and an explicit answer for every target. In an interview, mention the first-match rule, the `_` fallback and the compile error when nothing matches, then say what it replaces: the negated fallback, the E0428 overlap and, for many crates, a dependency on `cfg-if`.
- Is the code in a `cfg_select!` arm that was not chosen type-checked?No. The macro emits only the chosen arm's tokens, so the other arms are dropped before name resolution and type checking, just as a false `#[cfg]` removes an item; an arm calling a function that does not exist builds fine on targets that skip it. Each arm still has to be a well-formed expansion for the position, and CI must still build every target.
- What does rustc 1.99 say about an arm written after `_ =>`?It warns "unreachable configuration predicate" under the `unreachable_cfg_select_predicates` lint, which 1.99 puts in the `unused` group. Because `_` always matches, every later arm can never be chosen, which is almost always an ordering mistake.
- How does the standard library itself use `cfg_select!`?Its file-system layer selects one implementation module per platform family: an arm for `any(target_family = "unix", target_os = "wasi")` declares `mod unix; use unix as imp;`, a `target_os = "windows"` arm does the same with `windows`, and further arms cover other systems. The rest of that layer talks only to `imp`.
- cfg_select! expands every arm whose predicate is true, like several #[cfg] items.
- Without a _ arm, cfg_select! expands to nothing when no predicate matches.
- cfg_select! is evaluated at run time, like a match on the operating system.
- cfg_select! still needs the cfg-if crate or a nightly feature gate.
- Code in an arm that was not chosen is still type-checked, as with cfg!.
Assumes Rust 1.99; cfg_select! is stable since 1.95, and 1.99 warns about arms after _ through unreachable_cfg_select_predicates.