Paul Krill
Editor at Large

Rust introduces new Range types

news
Jun 30, 20262 mins

Rust 1.96.0 stabilizes a set of replacement range types that implement IntoIterator rather than Iterator, helping developers avoid the ‘footgun’ of implementing both Iterator and Copy.

Muskox. The family of musk oxen peacefully grazes on Wrangel Island in July. Mighty wild beasts. Overcast. Wrangel Island. Arctic. Arctic Ocean. The Chukchi Sea. Chukotka. Russian Far East.
Credit: Aleksandr Kutskii / Shutterstock

Rust 1.96.0 has arrived, bringing new Range* types to the programming language known for its memory safety.

Announced May 28, Rust 1.96.0 can be installed by current users by running the command rustup update stable.

In elaborating on the new Range* types, the Rust team said many users expect Range and related core::ops types to be Copy, but this is not the case. These types implement Iterator directly, so “it is a footgun to implement both Iterator and Copy on the same type”. RFC3550 proposed replacement range types that implement IntoIterator rather than Iterator, meaning they also can be Copy. The standard library portion of that RFC is now stable, introducing the types core::range::Range, core::range::RangeFrom, core::range::RangeInclusive, and associated iterators.

A future Rust version will add core::range::RangeFull and core::range::RangeTo as re-exports from core::ops. These do not implement Iterator and already implement Copy, the Rust team said. A future Rust version will also introduce core::range::legacy::* as the new home for the current ranges. Range syntax like 0..1 still produces the legacy types for now, the Rust team said, but will be updated to core::range types in an upcoming edition. With these stabilizations, it is now possible to store slice accessors in Copy types without splitting start and end, according to the team. Additionally, the new RangeInclusive type makes its fields public, unlike the legacy version that avoided exposing the exhausted iterator state.

Elsewhere in Rust 1.96.0, two new macros, assert_matches! and debug_assert_matches!, check that a value matches a given pattern, panicking with a Debug representation of the value otherwise. And WebAssembly targets no longer pass --allow-undefined to the linker, which means that undefined symbols when linking are now a linker error instead of being converted to WebAssembly imports from the "env" module. This change prevents modules from linking unless all linking-related symbols are defined to catch bugs earlier and prevent accidental issues with symbol naming or similar.

The Rust team on June 30 published a point release, Rust 1.96.1, which offers a series of fixes for Cargo, MIR, and libssh2:

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author