Back to Home

Rust 1.95.0: cfg_select!, if-let guards and stable APIs | Overview

Overview of the Rust 1.95.0 release: new features for developers. cfg_select! macro replaces cfg-if, support for if-let guards in match expressions, stabilization of key APIs. Practical examples and migration recommendations.

Rust 1.95.0: How new features simplify development
Advertisement 728x90

# Rust 1.95.0: How New Features Simplify Rust Development

Rust 1.95.0 stable has been released, expanding the language's capabilities for creating safe and efficient code. The main changes focus on simplifying conditional compilation, improving pattern matching syntax, and stabilizing critical APIs. These updates directly impact developers' daily work by reducing cognitive load and minimizing boilerplate code.

The cfg_select! Macro: A Built-in Alternative to cfg-if

The new cfg_select! macro solves the problem of platform-dependent implementations that previously required the external crate cfg-if. Now, conditional compilation at build time is integrated into the standard library. The macro works like match for cfg directives, selecting the first branch with a true predicate. This is especially useful for implementing cross-platform libraries, where different code is needed for Unix, Windows, or architectures.

Example usage:

Google AdInline article slot
cfg_select! {
    unix => {
        fn platform_specific() { /* Sale for Unix */ }
    }
    target_pointer_width = "32" => {
        fn platform_specific() { /* 32-bitnaya realizatsiya */ }
    }
    _ => {
        fn platform_specific() { /* Defolt */ }
    }
}

Key advantage: no dependency on external crates and unified syntax with other Rust macros. Note: the macro expands to a single branch at compile time, so unused code parts are completely excluded from the binary.

if-let Guards in match Expressions: Expanding Pattern Matching Capabilities

Rust 1.95.0 stabilizes if-let guards inside match expressions, allowing you to combine pattern matching and conditional checks in a single branch. This feature, previously available only in nightly versions, is now on the stable channel. The syntax remains concise and readable, avoiding nested match expressions.

Example:

Google AdInline article slot
match data {
    Some(x) if let Ok(y) = process(x) => {
        // Dostupny x and y
        handle(x, y);
    }
    _ => fallback(),
}

Important limitation: the compiler does not consider patterns in if-let guards during exhaustiveness checking. This means a branch with an if-let guard does not guarantee handling all possible cases, unlike a regular match. Developers should explicitly verify scenario coverage.

Stabilized APIs: Key Changes

The release stabilizes over 30 APIs affecting low-level operations and memory handling. Main groups of changes:

  • Uninitialized Memory Management: From, AsRef, AsMut methods for MaybeUninit<[T; N]> simplify working with raw buffers.
  • Atomic Operations: Added update and try_update for AtomicPtr, AtomicBool, AtomicInt, AtomicUint.
  • Collection Operations: New push_mut, insert_mut methods for Vec, VecDeque, LinkedList.
  • Low-Level Utilities: Layout::dangling_ptr, Layout::repeat, cold_path.

Of particular interest is the stabilization of bool: TryFrom<{integer}>, which allows safely converting integer types to booleans via TryFrom. This eliminates the need for manual checks like value != 0.

Google AdInline article slot

Deprecation of Custom Targets: What's Changed

Rust 1.95.0 removes support for custom JSON target specifications in the stable channel. This feature, previously available via the --target flag, required nightly tools even for basic standard library builds. Now, users with a fully stable toolchain won't encounter unexpected errors but will lose the ability to customize targets.

The Rust team is gathering use cases in the tracking issue to determine how to stabilize this feature in the future. For now, switch to official targets or use the nightly channel for experiments.

Key Takeaways

  • The cfg_select! macro replaces cfg-if in the standard library, eliminating external dependencies.
  • if-let guards in match expressions simplify complex checks but don't affect exhaustiveness checking.
  • Stabilization of atomic operations and memory methods is critical for systems programming.
  • Removal of custom targets in the stable channel requires reviewing custom builds.

Updating to Rust 1.95.0 is recommended for all projects where support for modern APIs and simplified conditional compilation matters. For testing future releases, use the beta channel (rustup default beta).

— Editorial Team

Advertisement 728x90

Read Next