Rust 1.95.0 Introduces cfg_select! Macro, if-let Guards in Matches, and More

By ⚡ min read

What's New in Rust 1.95.0

The Rust team has released version 1.95.0, bringing a handful of new features and stabilizations that make the language even more expressive and efficient. This release introduces a new cfg_select! macro, extends pattern matching capabilities with if-let guards, and stabilizes a wide range of APIs. If you already have Rust installed via rustup, simply run:

Rust 1.95.0 Introduces cfg_select! Macro, if-let Guards in Matches, and More
Source: blog.rust-lang.org
$ rustup update stable

If you haven’t installed Rust yet, head over to the official rustup page to get started. For those eager to test upcoming features, you can switch to the beta or nightly channels with rustup default beta or rustup default nightly—and please report any bugs you find!

cfg_select! Macro: Compile‑Time Configuration Made Simpler

One of the standout additions in Rust 1.95.0 is the cfg_select! macro, which acts as a compile‑time conditional expression. It works much like a match statement but evaluates configuration predicates (like those used with #[cfg()]) at compile time. This fulfills the same purpose as the popular cfg-if crate, but with its own syntax.

Here’s a quick example:

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",
};

The macro expands to the right‑hand side of the first arm whose condition evaluates to true. This makes conditional compilation cleaner and more readable, especially when you have multiple platform‑ or architecture‑specific blocks.

if-let Guards in match Expressions

Building on the let chains stabilised in Rust 1.88, version 1.95.0 now brings if-let guards directly into match expressions. This allows you to combine pattern matching with a conditional that itself performs pattern matching, all within a single arm.

For example:

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

Note that the compiler does not currently consider patterns used in if-let guards when checking exhaustiveness—just as with regular if guards. This feature makes code more concise and powerful, especially when dealing with nested results or options.

Stabilized APIs

Rust 1.95.0 also stabilizes a large set of APIs, many of which improve ergonomics around MaybeUninit, Cell, atomics, and collection types. Below is a full list:

Conversions and References for MaybeUninit and Cell Arrays

  • MaybeUninit<[T; N]>: From<[MaybeUninit<T>; N]>
  • MaybeUninit<[T; N]>: AsRef<[MaybeUninit<T>; N]>
  • MaybeUninit<[T; N]>: AsRef<[MaybeUninit<T>]>
  • MaybeUninit<[T; N]>: AsMut<[MaybeUninit<T>; N]>
  • MaybeUninit<[T; N]>: AsMut<[MaybeUninit<T>]>
  • [MaybeUninit<T>; N]: From<MaybeUninit<[T; N]>>
  • Cell<[T; N]>: AsRef<[Cell<T>; N]>
  • Cell<[T; N]>: AsRef<[Cell<T>]>
  • Cell<[T]>: AsRef<[Cell<T>]>

Atomic Operations

  • bool: TryFrom<{integer}>
  • AtomicPtr::update
  • AtomicPtr::try_update
  • AtomicBool::update
  • AtomicBool::try_update
  • AtomicIn::update
  • AtomicIn::try_update
  • AtomicUn::update
  • AtomicUn::try_update

New Modules and Core Additions

  • cfg_select! (macro)
  • mod core::range
  • core::range::RangeInclusive
  • core::range::RangeInclusiveIter
  • core::hint::cold_path

Pointer Methods

  • <*const T>::as_ref_unchecked
  • <*mut T>::as_ref_unchecked
  • <*mut T>::as_mut_unchecked

Collection Mutability Enhancements

  • Vec::push_mut
  • Vec::insert_mut
  • VecDeque::push_front_mut
  • VecDeque::push_back_mut
  • VecDeque::insert_mut
  • LinkedList::push_front_mut
  • LinkedList::push_back_mut

Get Started with Rust 1.95.0

To upgrade, simply run rustup update stable. For full details, check the detailed release notes. As always, the Rust community welcomes your feedback and contributions—happy coding!

Recommended

Discover More

How Go Handles Type Construction and Cycle Detection Behind the ScenesAccidental Heat Exposure May Ward Off Alzheimer's: The Story of Doug WhitneyTroubleshooting YouTube's High RAM Usage Bug: A Step-by-Step GuideThe Hidden Accessibility Crisis: How Session Timeouts Exclude Users with DisabilitiesRevolutionizing AI Communication: New Prompt Engineering Techniques Unlock LLM Potential