فا
← BACK TO THE WIRE
N°0303Rust2 MIN2 SOURCES

Rust 1.98 Turns Non-Zero Configuration Into a Compile-Time Check

Rust 1.98 stabilizes const-capable parsing for NonZero integers, giving protocol and systems developers a safer way to validate fixed non-zero configuration before runtime.

SHARE
Rust
Rust 1.98 Turns Non-Zero Configuration Into a Compile-Time Check
IMAGE: AI-GENERATED

Rust 1.98.0, released on August 20, adds a small standard-library capability with a useful systems consequence: NonZero::<T>::from_str_radix is now stable and const for the supported integer types.

The method parses a string in an explicitly selected base and returns a Result<NonZero<T>, ParseIntError>. Unlike ordinary integer parsing, a successful result carries a type-level guarantee that the value is not zero. That makes it a natural fit for configuration such as shard counts, page sizes, retry limits, table capacities, and protocol identifiers where zero is invalid by design.

The important change is the const qualification. A fixed value can now be parsed and checked while compiling:

rust
use std::num::NonZeroU32;

const SHARD_COUNT: NonZeroU32 = match NonZeroU32::from_str_radix("16", 10) {
    Ok(value) => value,
    Err(_) => panic!("invalid non-zero shard count"),
};

This moves a class of configuration mistakes from startup into the build. If the literal becomes "0", contains an invalid digit, or cannot be represented by the target integer type, compilation can fail at the constant definition instead of leaving the application to discover the problem later.

Runtime inputs still use the same API:

rust
fn parse_limit(input: &str) -> Result<NonZeroU32, std::num::ParseIntError> {
    NonZeroU32::from_str_radix(input, 10)
}

For ICP and other protocol-oriented Rust code, the value is useful at the boundary: parse external text once, reject invalid or zero values, and pass NonZeroU32 deeper into the program rather than carrying a plain integer plus a repeated assumption. This does not replace business-level bounds checks; a non-zero limit can still be far too large.

The safety-relevant API details matter. from_str_radix accepts only a radix from 2 through 36; an out-of-range radix causes a panic. It also rejects leading or trailing non-digit characters, including whitespace, and rejects underscores even though underscores are allowed in Rust numeric literals. These are documented API behaviors, so callers should normalize or validate input before selecting the radix and should avoid treating the method as permissive parsing.

The broader lesson of Rust 1.98 is not that every configuration value should become a constant. It is that an existing domain invariant—“this integer must not be zero”—can now be represented during compile-time parsing without a third-party crate. That is a modest ergonomic improvement, but in protocol code modest invariants are often the ones that prevent ambiguous initialization and delayed failures.

TAGSRustRust 1.98Standard LibrarySystems Programming
Grounded sources2 REFS
  1. [01]Announcing Rust 1.98.0blog.rust-lang.org
  2. [02]NonZero in std::num — Rust 1.98.0 documentationdoc.rust-lang.org
Read next

Get the wire in your inbox

Every new signal, straight from the generator. No noise, unsubscribe anytime.

RSS AVAILABLE · NO SPAM