Rust 1.95.0 Release: cfg_select! Macro, Improved Match Guards, and More

By

Overview of Rust 1.95.0

The Rust team is thrilled to announce the release of Rust 1.95.0, the latest stable version of the language renowned for enabling developers to build efficient and reliable software. If you already have Rust installed via rustup, you can upgrade seamlessly with the command:

Rust 1.95.0 Release: cfg_select! Macro, Improved Match Guards, and More
Source: blog.rust-lang.org
$ rustup update stable

New to Rust? You can install rustup from the official website. For detailed changes, consult the release notes. Developers eager to test upcoming features can switch to the beta or nightly channels using rustup default beta or rustup default nightly. Please report any issues you encounter!

New Features in Rust 1.95.0

cfg_select! Macro: Streamlined Conditional Compilation

One of the headline features in Rust 1.95.0 is the introduction of the cfg_select! macro. This macro provides a compile-time conditional selection mechanism, similar to the popular cfg-if crate but with its own syntax. It evaluates a series of configuration predicates and expands to the right-hand side of the first arm that evaluates to true.

Here are two practical examples:

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

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

This macro simplifies platform-specific code and reduces boilerplate, making it easier to maintain cross-platform projects.

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 arms. You can now use if let guards within match expressions to add extra conditional logic based on pattern matching. 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 currently does not consider patterns used in if let guards when checking exhaustiveness, similar to how if guards work. This feature allows more expressive and concise matching logic.

Stabilized APIs and Enhancements

Rust 1.95.0 brings a wealth of stabilized APIs, enhancing the standard library’s functionality. Key additions include:

Improved Conversions for MaybeUninit and Cell

  • MaybeUninit<[T; N]>: Implemented From<[MaybeUninit<T>; N]>, AsRef and AsMut for both [MaybeUninit<T>; N] and [MaybeUninit<T>].
  • [MaybeUninit<T>; N]: Now implements From<MaybeUninit<[T; N]>>.
  • Cell: Added AsRef implementations for Cell<[T; N]> and Cell<[T]>.

These additions simplify conversions between array and slice representations of MaybeUninit and Cell.

bool: TryFrom<{integer}>

The bool type now implements TryFrom for all integer types, allowing safe conversion from integers (0 becomes false, 1 becomes true, any other value returns an error). This eliminates manual checks and improves code clarity.

Atomic Operations: update and try_update

Several atomic types gain update and try_update methods: AtomicPtr, AtomicBool, and all AtomicIn/AtomicUn (atomic integer and unsigned integer types). These methods provide a convenient way to atomically modify the value using a closure, with try_update returning a boolean indicating success.

Additions to core::range and core::hint

  • A new core::range module has been stabilized, including core::range::RangeInclusive and core::range::RangeInclusiveIter.
  • core::hint::cold_path is now stable, allowing developers to hint that a code path is unlikely to be taken, aiding branch prediction.

Unchecked Pointer Dereference Methods

Raw pointers *const T and *mut T now have as_ref_unchecked and as_mut_unchecked methods (for *mut T). These offer a way to dereference pointers without safety checks, useful in performance-critical sections where invariants are upheld by the caller.

New Methods for Vec, VecDeque, and LinkedList

  • Vec: Added push_mut and insert_mut.
  • VecDeque: Added push_front_mut, push_back_mut, and insert_mut.
  • LinkedList: Added push_front_mut (and likely other related methods as per the original release notes).

These methods provide mutable access to elements when pushing or inserting, enabling direct modification without extra steps.

Conclusion

Rust 1.95.0 delivers substantial quality-of-life improvements for Rust developers. The cfg_select! macro and enhanced match guards make conditional compilation and pattern matching more powerful. The stabilized APIs expand the standard library’s versatility, especially in areas like memory management and concurrency. As always, the Rust team encourages you to test upcoming releases via the beta and nightly channels and report any issues. Happy coding!

Tags:

Related Articles

Recommended

Discover More

Machine-Speed Attacks Force Cybersecurity Rethink: Automation and AI Now Critical for DefenseFrom Prompt to Production: Building an AI-Powered Coaching SaaS with MeDoPragmata Sales Skyrocket: Capcom's New IP Breaks Records in 48 HoursHow to Build Digital Twins for Instant Market Research: A Step-by-Step Guide10 Key Facts About NEAR Intents' Expanded Crosschain Swaps to Zcash