Series: The Rust Annals

Vol. I Issue 96 nlopes.dev

Announcing Rust 1.95.0

Rust 1.95 adds cfg_select! for selecting code by configuration and if let match guards for conditionals based on pattern matching. It also stabilizes library APIs and additional const uses, while removing stable support for custom JSON target specifications—a change that should not affect users of fully stable toolchains.

Select the first matching configuration

The new cfg_select! macro acts roughly similar to a compile-time match on cfgs. It fulfills the same purpose as the popular cfg-if crate, although with a different syntax: it expands to the right-hand side of the first arm whose configuration predicate evaluates to true.

The examples show selection between function definitions and between expressions:

cfg_select! {
    unix => {
        fn foo() { /* unix specific functionality */ }
    }
    target_pointer_width = "32" => {
        fn foo() { /* non-unix, 32-bit functionality */ }
    }
    _ => {
        fn foo() { /* fallback implementation */ }
    }
}

let is_windows_str = cfg_select! {
    windows => "windows",
    _ => "not windows",
};

Match guards gain pattern matching

Rust 1.88 stabilized let chains. Rust 1.95 brings that capability into match expressions, allowing for conditionals based on pattern matching. Bindings from the arm pattern and its if let guard are available in the arm body:

match value {
    Some(x) if let Ok(y) = compute(x) => {
        // Both `x` and `y` are available here
        println!("{}, {}", x, y);
    }
    _ => {}
}

The compiler will not currently consider the patterns matched in if let guards as part of the exhaustiveness evaluation of the overall match, just like if guards.

Previously stable APIs gain const support

Alongside the newly stabilized library APIs, these previously stable APIs are now stable in const contexts:

  • fmt::from_fn
  • ControlFlow::is_break
  • ControlFlow::is_continue

Custom JSON target specifications leave stable

Rust 1.95 removes support on stable for passing a custom target specification to rustc. This should not affect any Rust users using a fully stable toolchain, as building the standard library (including just core) already required using nightly-only features.

The Rust team is gathering use cases for custom targets on the tracking issue as it considers whether some form of this feature should eventually be stabilized.

Reproduced from the Rust blog under its publication licence. Typeset in Literata.