Rust 1.95.0: Key New Features and API Stabilizations

From Xshell Ssh, the free encyclopedia of technology

Welcome to the latest stable release of Rust, version 1.95.0! This update brings a handful of powerful new features designed to simplify conditional compilation, enhance pattern matching, and expand the standard library. Whether you're a seasoned Rustacean or just getting started, these improvements aim to make your code more expressive and reliable. Read on for a detailed Q&A covering the most important changes.

How can I install or upgrade to Rust 1.95.0?

If you already have Rust installed via rustup, getting 1.95.0 is as simple as running:

Rust 1.95.0: Key New Features and API Stabilizations
Source: blog.rust-lang.org
$ rustup update stable

If you don’t have Rust installed yet, head over to the official website to download rustup, which will set you up with the latest stable compiler and tools. For those interested in testing future releases, you can switch to the beta or nightly channels using rustup default beta or rustup default nightly. Remember to report any bugs you encounter!

What is the new cfg_select! macro and how does it work?

Rust 1.95.0 introduces cfg_select!, a compile-time macro that acts like a match statement for configuration predicates (like target_os, unix, etc.). It evaluates the first arm whose condition is true and expands to that arm's right-hand side. This eliminates the need for multiple #[cfg(...)] attributes and provides a clean, readable alternative to the popular cfg-if crate. For example:

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

The macro works with any cfg predicate, including custom ones set via --cfg. This makes conditional compilation more concise and less error-prone.

How do if-let guards work 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 additional conditions. For instance:

match value {
    Some(x) if let Ok(y) = compute(x) => {
        println!("{}, {}", x, y);
    }
    _ => {}
}

This syntax allows you to bind variables from both the initial pattern and the guard condition in one arm. Note that the compiler currently does not consider these guards when checking exhaustiveness, similar to regular if guards. This feature simplifies complex conditional logic without needing extra nesting or helper functions.

What are some of the most notable stabilized APIs in this release?

Rust 1.95.0 stabilizes a wide range of APIs that enhance memory safety, atomic operations, and collection manipulation. Key additions include:

  • MaybeUninit<[T; N]> — now implements From, AsRef, and AsMut for array conversions, making uninitialized array handling more ergonomic.
  • Atomic operationsAtomicPtr, AtomicBool, and all atomic integer types now have update and try_update methods, providing a safe way to atomically modify values.
  • New core modulescore::range::RangeInclusive and its iterator are stabilized, along with core::hint::cold_path for hinting the optimizer about cold code paths.
  • Pointer methods<*const T>::as_ref_unchecked and <*mut T>::as_ref_unchecked (and as_mut_unchecked) allow creating references from pointers without safety checks, for use in low-level code.
  • Collection mutationsVec::push_mut, Vec::insert_mut, and similar methods for VecDeque and LinkedList enable in-place mutation of single elements.
  • bool: TryFrom<{integer}> — a safe conversion from integers to bool (0 becomes false, non-zero becomes true).

These additions reduce boilerplate and improve safety across many common patterns.

How does cfg_select! compare to the existing cfg-if crate?

The cfg-if crate has been a popular way to write conditional compilation blocks without nesting #[cfg] attributes. Rust 1.95.0's cfg_select! macro serves the same purpose but with a slightly different syntax. While cfg-if uses a function-like macro with if cfg!(...) style conditions, cfg_select! uses a more direct pattern-matching approach that mirrors Rust's match syntax. Both macros expand to the appropriate code based on compile-time predicates. The advantage of cfg_select! is that it is built into the standard library, eliminating an external dependency. It also works seamlessly with any cfg predicate, including custom ones. For existing projects, migrating is straightforward, but the cfg-if crate remains fully supported.

Are there any other important changes in this release?

Beyond the headline features, Rust 1.95.0 includes several minor improvements and bug fixes. The cfg_select! macro is now exported from core and std, making it universally accessible. The standard library also sees stabilizations like Cell<[T; N]> implementing AsRef for both fixed-size and slices, and the addition of LinkedList::push_front_mut and push_back_mut for in-place mutation of linked list elements. Developers working with concurrency will appreciate the new atomic update and try_update methods, which provide a convenient way to perform read-modify-write operations without looping manually. For a complete list, refer to the detailed release notes on the Rust blog.