Mara Bos Profile picture
6 May, 10 tweets, 5 min read
The new stable version of @rustlang, Rust 1.52, was released just now! 🦀🎉

This release contains quite a few new small but significant features.

A thread.

My favourite new addition is `str::split_once`.

We already had str::split and str::splitn, which result in an iterator. But when parsing something simple, you often want to split something exactly once. For example, to parse a string containing `key=value`.

2/10   let s = "hello=world";   let (key, val) = s.spliScreenshot of the split_once and rsplit_once documentation.
Another one I'm excited about is one of the first features I worked on: std::fmt::Arguments::as_str()

fmt::Arguments is returned by format_args!(), and appears in macros that support formatting.

as_str() allows handling the literal case without special-casing your macro:

3/10 // Before  macro_rules! log {     ($msg:literal) => {       // After  macro_rules! log {     ($($args:tt)*) => {
Next: #[deny(unsafe_op_in_unsafe_fn)]

This forces you to write `unsafe` to call unsafe functions, even when inside an unsafe fn. Most unsafe functions have a mostly (or fully) safe implementation, so separating 'unsafe to call' from 'unsafe implementation' is often useful.

4/10 #![deny(unsafe_op_in_unsafe_fn)]  pub unsafe fn dangerous()
The casting operator (`as`) already allowed converting from a reference to an array to a pointer to the first element (from &[T; N] to *const T). Starting in Rust 1.52, this also works for the mutable case (from &mut [T; N] to *mut T):

5/10     let mut array = [1, 2, 3];     let ptr = &mut array as *
Another important library feature: slice::partition_point.

This performs a binary search through a slice, given a (boolean) predicate.

With `<= x` or `< x` as the predicate, this is equivalent to the `upper_bound` and `lower_bound` algorithms of the C++ standard library.

6/10 Screenshot of the partition_point documentation.  Returns th
If you like reference-counted errors, Rust 1.52 now implements the Error trait for all Arc<T> for which T itself implements Error:

7/10 #[stable(feature = "arc_error", since = "1.52
As in most recent Rust releases, more functions are turned into `const fn`s to allow use in compile-time evaluated expressions.

This time it's the ascii upper-/lowercase functions, char::len_utf8 and len_utf16, and all the integer division/remainder functions:

8/10 These are now const fn:  char::len_utf8 char::len_utf16 char
The last new feature I want to highlight is a Rustdoc feature: Task lists.

With the same syntax that's supported by some other markdown variants (e.g. GitHub's), you can now add checked and unchecked checkboxes in your documentation:

9/10 //! My awesome crate. //! //! This crate is not yet finishedScreenshot of generated documentation:  My awesome crate.  T
And with that, I'm ending this thread. :)

There's a few more changes and additions, which you can find in the full release notes:…


• • •

Missing some Tweet in this thread? You can try to force a refresh

Keep Current with Mara Bos

Mara Bos Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!


Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @m_ou_se

24 Apr
I just approved the PR for a very exciting addition to @rustlang 1.53: IntoIterator for arrays 🎉🦀

Before this change, only references to arrays implemented IntoIterator, which meant you could iterate over &[1,2,3] and &mut [1,2,3], but not over [1,2,3] directly.

1/6 error[E0277]: `[{integer}; 3]` is not an iterator  borrow thfor e in [1, 2, 3] { // Works in 1.53!     println!("{}
The reason we didn't add it sooner was backwards compatibility. `array.into_iter()` already compiles today, because of the way methods get resolved in Rust. This implicitly calls `(&array).into_iter()`. Adding the trait implementation would change the meaning and break code.

2/6 for e in [1, 2, 3].into_iter() {     // Surprise: `e` is a r
Technically we consider this type of breakage (adding a trait impl) 'minor' and acceptable. But there was too much code that would be broken by it. Thanks to @LukasKalbertodt, such code results in a warning nowadays, but there's a lot of code that just doesn't get updated.

3/6 warning: this method call currently resolves to `<&[T; N] as
Read 6 tweets
22 Apr
Lots of new standard library additions will become stable in @rustlang 1.53. 🦀🎉

1. Duration::ZERO, Duration::is_zero(), Duration::MAX pub const ZERO: Duration  A...
2. Duration::saturating_{add,sub,mul}

Since Durations cannot represent negative values, saturating_sub might come in handy to avoid panics. pub fn saturating_add(self, fn saturating_sub(self,...
Read 17 tweets

Did Thread Reader help you today?

Support us! We are indie developers!

This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!