Rust Community Explores Physics and Units in Type System
The Rust community is discussing a proposal to integrate native support for physical units like meters and seconds directly into the language's type system. This feature would provide compile-time guarantees against unit-mismatch errors, a common source of bugs in scientific and systems-level software. Debate also continues around whether raising the minimum supported Rust version (MSRV) is a breaking change.
- The proposal for native unit support in Rust's type system aims to prevent a class of errors exemplified by NASA's 1999 Mars Climate Orbiter failure, which was lost due to a software mismatch between metric and imperial units. This incident cost $125 million and is a frequently cited motivation for unit safety in programming. - Integrating units of measure into a language's type system is not a new concept; languages like F# have existing first-class support for it. The goal is to make expressions that incorrectly mix units, like adding a distance to a duration, a compile-time error rather than a runtime bug. - The Rust ecosystem already has several community-developed libraries, known as crates, for handling physical units, such as `uom`, `dimensioned`, and `physical-units`. These libraries use Rust's existing type system to provide zero-runtime-cost dimensional analysis. - The debate over whether increasing the Minimum Supported Rust Version (MSRV) constitutes a "breaking change" is a long-standing issue in the community. One side argues that if a `cargo update` breaks a user's build because their compiler is too old, it is effectively a breaking change. - The opposing viewpoint is that treating MSRV bumps as major semantic versioning changes would hinder the ecosystem's ability to adopt new language features and could lead to a fragmented landscape of library versions. - To address this, an RFC introduced a `rust-version` field in the `Cargo.toml` file, allowing crates to formally specify their MSRV. This enables Cargo to check a crate's MSRV requirements before attempting to build it, preventing build failures due to an outdated compiler. - The MSRV discussion is particularly relevant for developers working with Linux distributions that often package older, stable versions of the Rust compiler. This can create friction with a crate ecosystem that may move at a faster pace, adopting features from more recent Rust releases.