Mara Bos Profile picture
Rust dev, Electronics engineer, Founder/CTO of Fusion Engineering, @rustlang Library team lead, ADHD, Polyamorous, Lesbian, She/Her

Apr 22, 2021, 17 tweets

Lots of new standard library additions will become stable in @rustlang 1.53. 🦀🎉

Thread:

1. Duration::ZERO, Duration::is_zero(), Duration::MAX

2. Duration::saturating_{add,sub,mul}

Since Durations cannot represent negative values, saturating_sub might come in handy to avoid panics.

3. Option::insert

`opt.insert(1)` does the same as `opt = Some(1)`, but also gives you a mutable reference to the `1` inside that Some after it's stored in the Option.

4. {integer}::BITS

I already promised this one for Rust 1.51, but we had to temporarily revert it. Trying again now for 1.53 :)

5. BufReader::seek_relative

Unlike the seek() function from the Seek trait, this one does not return the current position in the stream, which allows the BufReader to perform the seek without accessing the underlying reader if the new position lies within the buffer.

6. NonZero*::leading_zeros() and NonZero*::trailing_zeros()

These avoid the zero-check that the same functions on the regular integer types need to do, since on many architectures, the corresponding native instruction doesn't handle the zero case.

These are both `const fn`s.

7. std::array::from_ref and std::array::from_mut

These can safely convert a &T to &[T; 1] or &mut T to &mut [T; 1].

8. f32::is_subnormal and f64::is_subnormal

A way to easily check if a floating point number is subnormal.

9. DebugStruct::finish_non_exhaustive()

This allows you to mark the custom Debug representation of your type with fmt.debug_struct() to not be exhaustive. It adds two dots before the closing brace.

We've also updated the std types' Debug impls to consistently use this.

10. AtomicPtr::fetch_update and AtomicBool::fetch_update

We already had this one on the other atomic types, but they were missing on atomic pointers and booleans.

11. Peekable::peek_mut()

This allows you to mutate the peeked value which will be returned next by the Iterator.

12. OsStr::{make_ascii_lowercase, make_ascii_uppercase, to_ascii_lowercase, to_ascii_uppercase}

We already had these on `str`s, but these new ones can be applied to an OsStr directly even if it's not valid UTF-8.

13. std::cmp::{min_by, max_by, min_by_key, max_by_key}

Like `std::cmp::{min, max}`, but allows you to specify the way the values are compared.

14. io::ErrorKind::Unsupported

A new ErrorKind which is used when the operation is not supported by the system. (E.g. ENOSYS on Unix and ERROR_CALL_NOT_IMPLEMENTED on Windows.)

Previously these fell under io::ErrorKind::Other.

15. BTreeMap::retain and BTreeSet::retain

Just like Vec::retain, these allow you to filter out some entries from the collection based on a predicate.

In case of the BTreeMap, you get access to both the key and the value, and can mutate the values if needed.

And that's the end of this thread. :)

Thanks to everyone involved in the design, implementation and stabilization of all these features! 💛

I probably missed a few stabilizations, and there will be even more since Rust 1.53 is still under development. So stay tuned. 🙂

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling