Rust 1.95.0 Released: Introducing cfg_select! and Improved Pattern Matching

What's New in Rust 1.95.0

The Rust team has announced the release of version 1.95.0, bringing significant enhancements to the language's conditional compilation and pattern matching capabilities. This update focuses on reducing boilerplate while maintaining Rust's core safety guarantees. Developers will find the new cfg_select! macro and improved match expressions particularly valuable for writing cleaner, more maintainable code.

Rust 1.95.0 Released: Introducing cfg_select! and Improved Pattern Matching
Source: blog.rust-lang.org

Updating to Rust 1.95.0

If you already have Rust installed via rustup, obtaining the latest stable release is straightforward:

$ rustup update stable

New users can install rustup from the official Rust website. For those interested in testing upcoming features, you can switch to the beta or nightly channels:

$ rustup default beta
$ rustup default nightly

We encourage all users to report any bugs they encounter on the GitHub issue tracker.

The cfg_select! Macro

Conditional compilation has long been a necessity in Rust, especially for cross-platform development. The cfg-if crate has been a popular solution, but Rust 1.95.0 now offers an official alternative: cfg_select!. This macro works like a compile-time match on configuration predicates, expanding to the right-hand side of the first matching arm.

Basic Usage

The macro accepts any number of arms, each consisting of a configuration predicate and a corresponding expression or block:

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

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

The underscore (_) serves as a catch-all, similar to the default arm in a match expression. This feature streamlines conditional code without relying on external crates.

if-let Guards in Match Expressions

Building on the let chains stabilized in Rust 1.88, version 1.95.0 extends this capability to match expressions. You can now use if let guards to combine pattern matching with conditional logic inside a single match arm:

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

This syntax keeps your code concise and expressive, eliminating the need for nested if let statements. Note that the compiler currently does not consider patterns in if let guards when checking exhaustiveness, so you may need to add a wildcard arm.

Stabilized APIs

Rust 1.95.0 stabilizes a number of APIs that improve ergonomics and safety. Here is a categorized overview:

Conversions and References for MaybeUninit and Cell

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

Integer to Boolean Conversion

  • bool: TryFrom<{integer}>

Atomic Operations

  • AtomicPtr::update and AtomicPtr::try_update
  • AtomicBool::update and AtomicBool::try_update
  • AtomicIn::update and AtomicIn::try_update
  • AtomicUn::update and AtomicUn::try_update

Core Range and Hint

  • mod core::range with core::range::RangeInclusive and core::range::RangeInclusiveIter
  • core::hint::cold_path

Raw Pointer Safety

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

Collection Mutators

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

For a complete list, refer to the detailed release notes.

Conclusion

Rust 1.95.0 continues the language's tradition of empowering developers with safe, efficient tools. The cfg_select! macro reduces reliance on third-party crates, while if-let guards in match expressions make pattern matching more flexible. Combined with the newly stabilized APIs, this release is a solid step forward for the ecosystem. Update today and give these features a try!

Tags:

Recommended

Discover More

vl88win798 Reasons to Ditch Cloud-Based IR Control and Embrace a Local SolutionHow to Snag Today’s Best Android App and Game Deals: A Step-by-Step Guidewin7910 Key Developments in OpenSearch's Evolution Into an AI Data LayerThe Man Behind Tropicana's Costly Rebrand Takes on the U.S. Government: A Two-Month Challenge32wintobet32winxoilacKubernetes v1.36: In-Place Pod-Level Resource Scaling Hits Beta, Here's What You Need to Knowtobetxoilacvl88