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

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

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

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

Mastering GDB's Source-Tracking Breakpoints: A Step-by-Step Guide5 Key Power Moves in the AI Compute War: How Anthropic and Musk Forced Altman's HandA Detailed Guide to Analyzing Spiral Galaxy NGC 3137 from Hubble DataFinding Your Product's Core: A Step-by-Step Guide to Building StickinessGo 1.26 Revolutionizes Code Modernization with Rewritten 'go fix' Command