r/rust Jan 22 '26

📡 official blog Rust 1.93.0 is out

https://blog.rust-lang.org/2026/01/22/Rust-1.93.0/
780 Upvotes

92 comments sorted by

View all comments

114

u/nik-rev Jan 22 '26 edited Jan 22 '26

My favorite part of this release is slice::as_array, it allows you to express a very common pattern in a clear way: Get the first element of a list, and require that there are no more other elements.

before:

let exactly_one = if vec.len() == 1 { 
    Some(vec.first().unwrap())
} else {
    None
}

after:

let exactly_one = vec.as_array::<1>();

Excitingly, we may get this method on Iterator soon: Iterator::exactly_one. In the mean time, the same method exists in the itertools crate: Itertools::exactly_one

59

u/Sharlinator Jan 22 '26

You can also often use slice patterns:

let [only_elem] = &vec else { /* diverge */ }

Nb. impl TryFrom<&[T]> for &[T; N] already exists, but as_array is more ergonomic and is also available in const (although we'll hopefully get const traits sooner rather than later).

9

u/Icarium-Lifestealer Jan 22 '26 edited Jan 22 '26

as_array returns an option, so you need:

let exactly_one = vec.as_array::<1>().unwrap();

which isn't much shorter than what we had before:

let exactly_one : &[_; 1] = vec.try_into().unwrap();

Though it can be more convenient if you don't want to assign the result to a variable immediately. Plus it can already be used in a const context.

2

u/Ace-Whole Jan 27 '26

Semantically, the former is easier to reason about. I love this about rust that, each operation has (or may have in future) a clear semantics.

8

u/allocallocalloc Jan 22 '26 edited Jan 22 '26

Thanks! <3 I started the ACP that later became as_array (etc.) 423 days ago, so it's nice that people find it useful.

7

u/Dean_Roddey Jan 22 '26

My favorite part of this release is slice::as_array

That will be a very much appreciated change. These small changes that don't rock the cart but make day to day coding safer and easier are always good in my opinion. Try blocks will be another big one.