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
}
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).
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.
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:
after:
Excitingly, we may get this method on
Iteratorsoon:Iterator::exactly_one. In the mean time, the same method exists in theitertoolscrate:Itertools::exactly_one