Mara Bos Profile picture
Oct 21, 2021 β€’ 17 tweets β€’ 7 min read β€’ Read on X
A few hours ago, @rustlang 1.56 was released! πŸ¦€

This version ships with the new edition: Rust 2021! 🐊✨🎊

There's quite a few new features in the new version and edition:

1/17
Starting today, `cargo new` will use `edition = "2021"`. You can migrate your 2018 crates with `cargo fix --edition`.

These are all the edition changes:

1. `array.into_iter()` now iterates by value, instead of giving references.

(See for details.)

2/17 for (i, x) in [1, 2, 3].into_iter().enumerate() {        //
2. Closures in Rust 2021 will capture only the fields of an object you use, instead of the entire object. This should result in fewer fights with the borrow checker:

3/17 let mut x = ("a".to_string(), "b".to_str
3. In Rust 2021, `$x:pat` in macro_rules now accepts patterns that include a `|`, making your macro rules easier to write:

4/17 // Before, in Rust 2018: macro_rules! assert_match {     ($e
4. Specifying `edition = "2021"` in your Cargo.toml implies `resolver = "2"`, when not explicitly given.

See the announcement of Rust 1.51 for details on Cargo's new resolver: blog.rust-lang.org/2021/03/25/Rus…

5/17 [package] name = "example" version = "1.0.0&q
5. In Rust 2021, you don't have to import the TryFrom, TryInto and FromIterator traits anymore. They are now part of the prelude, which is automatically imported into all your modules:

6/17 // No imports necessary!  fn f(x: u32) -> (i32, Vec<u8>) {
6. The panic!() (and assert) macros in Rust 2021 no longer behave inconsistently when given only one argument. This unblocks a feature that will be available in a few versions from now: implicit format arguments.

7/17 panic!("hey {}"); // Missing an argument like this
7. We reserved some syntax for identifiers and literals with prefixes in Rust 2021. This syntax doesn't mean anything *yet*, but reserving it means we can start giving meaning to it in the (near?) future:

8/17 // These are all syntax errors for now! These are just some
And finally the last change that's part of the 2021 edition:

8. Some old syntax that's been deprecated for a while is completely removed in Rust 2021. This means we can use that syntax for something else in the future. (Maybe we can use `...` for variadic generics.)

9/17 match x {     0...5  => a(), // This is now a hard error!
Then let's continue with the changes in Rust 1.56 that are available in all editions:

1. Extend<(A, B)> for (Extend<A>, Extend<B>)

It allows splitting an iterator over tuples into separate collections, a bit like the opposite of .zip():

10/17 let mut a = (Vec::new(), String::new());  let c = vec![(1, '
2. From<array> for BTreeMap, BTreeSet, HashMap, HashSet, VecDeque, LinkedList, and BinaryHeap.

You can now use e.g. BTreeMap::from(..) to make a collection with directly the contents you want:

11/17 let nums = BTreeMap::from([     (1, "one"),     (2
3. You can now combine `@` bindings with regular pattern bindings. That means you can now give a name to an object _and give a name to some parts of it_ at the same time:

12/17 let tuple @ (first, _) = (1, 2);  assert_eq!(first, 1); asse
4. BufWriter::into_parts()

BufWriter::into_inner() will try to flush the buffer before giving you back the underlying Write object, which can fail.

BufWriter::into_parts() cannot fail and gives you the Write object and the unflushed buffer, so you can handle it manually:

13/17 pub fn into_parts(self) -> (W, Result<Vec<u8>, WriterPanicke
5. A new .shrink_to() method on Vec, String, PathBuf, VecDeque, HashSet, etc.

This allows you to *reduce* the .capacity() of a collection. It is basically the opposite of .reserve():

14/17 pub fn shrink_to(&mut self, min_capacity: usize)  Shrinks th
6. const mem::transmute() 😬

You can now use std::mem::transmute to do horrible things in a const fn:

15/17 // This compiles now, but is still a bad idea!  const fn f(x
And finally, a new Cargo feature:

7. You can now specify the minimum supported Rust version in your Cargo.toml:

rust-version = "1.56.0"

If specified, Cargo will give users of your crate a clear error when their version of Rust is too old:

16/17 $ cat Cargo.toml [package] name = "example" versio
And that's the end of this thread!✨

For a more complete list of changes in Rust 1.56, check the release notes:

Rust: github.com/rust-lang/rust…
Cargo: github.com/rust-lang/carg…
Clippy: github.com/rust-lang/rust…

And for details on the 2021 edition, see: doc.rust-lang.org/edition-guide/

17/17

β€’ β€’ β€’

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!

PDF

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

Dec 15, 2022
πŸ†•πŸ¦€ Just an hour ago, #rustlang 1.66.0 was released!

As usual, here's a thread with some of the highlights. 🧡

1/12
Rust 1.66 comes with std::hint::black_box(), a function that does nothing. However, the compiler tries its very best to pretend it doesn't know what it does.

It is useful in benchmarks, to prevent the compiler from optimizing your entire benchmark away.

2/12 use std::hint::black_box; use std::sync::atomic::{AtomicU64,
The Option type got a new method: Option::unzip(). It's basically the opposite of Option::zip(): it splits an Option of a pair into a pair of Options.

3/12 impl<T, U> Option<(T, U)>  pub fn unzip(self) -> (Option<T>,
Read 12 tweets
Nov 3, 2022
πŸ†•πŸ¦€ About an hour ago, @rustlang 1.65.0 was released.

As is tradition, here's a thread with some of the highlights. 🧡

1/10
Today's Rust release contains a long-awaited feature: generic associated types (GATs). πŸŽ‰

This allows associated types to be generic, which unlocks a lot of useful patterns.

See the blog post about the stabilization of this feature for details: blog.rust-lang.org/2022/10/28/gat…

2/10 trait LendingIterator {     type Item<'a> where Self: 'a;
Another big new feature in today's Rust release is let-else statements.

You can now write things like:

let Ok(a) = i32::from_str("123") else { return };

without needing an if or match statement. This can be useful to avoid deeply nested if statements.

3/10 fn parse_key_value(s: &str) -> Result<(&str, i32), ParseErro
Read 10 tweets
Sep 22, 2022
πŸ†•πŸ¦€ A few hours ago, @rustlang 1.64.0 was released! πŸŽ‰

Just like every six weeks, at every new release, here's a thread with some of the highlights. 🧡

1/15

blog.rust-lang.org/2022/09/22/Rus…
Rust now has a new async-related trait: IntoFuture.

The .await syntax be used on anything that implements IntoFuture. (Similar to how, with a for loop, you can iterate over anything that implements IntoIterator.)

This allows types to provide easier async interfaces.

2/15 use std::future::{ready, In...
Today's Rust release also comes with two more async-related tools:

The std::future::poll_fn function allows you to easily create a future from a closure (like iter::from_fn for iterators).

The std::task::ready!() macro extracts a Poll::Ready, or returns early on Pending.

3/15 let f = future::poll_fn(|cx...
Read 15 tweets
Aug 11, 2022
πŸ†•πŸ¦€ Just moments ago, @rustlang 1.63.0 was released! πŸŽ‰

It's quite a big release, with even more exciting new features than usual!

Here's a thread with some of the highlights. 🧡

1/16

blog.rust-lang.org/2022/08/11/Rus…
One of the features I'm most excited about is scoped threads! (Although I'm obviously biased, since I worked on this myself.)

As of today, you can use std::thread::scope() to spawn threads that borrow local variables, reducing the need for Arc! ✨

doc.rust-lang.org/stable/std/thr…

2/16 let mut a = vec![1, 2, 3]; let mut x = 0;  std::thread::scop
Another thing I'm very excited about, is that Mutex, RwLock and Condvar now all have a _const_ new function.

This means you can now have a static Mutex without having to use lazy_static or once_cell. ✨

3/16 use std::sync::Mutex;  static S: Mutex<String> = Mutex::new(
Read 16 tweets
Jun 30, 2022
πŸ†•πŸ¦€ Just moments ago, @rustlang 1.62.0 was released! πŸŽ‰

As usual, a thread with some of the highlights. 🧡

1/9

blog.rust-lang.org/2022/06/30/Rus…
Cargo now has 'cargo add' built-in: a (sub)command to add a crate to your Cargo.toml. It automatically looks up the latest version, and shows you the available features of the crate.

See `cargo add --help` for more details.

2/9 $ cargo add rand     Updating crates.io index       Adding r
On Linux and several BSDs, std::sync's Mutex, RwLock, and Condvar now no longer do any allocations. They used to be (heap-allocated) wrappers around pthread lock types, but have been replaced by a minimal, more efficient, futex-based implementations.

3/9

Read 9 tweets
May 16, 2022
πŸ¦€ As of Rust 1.62 (going into beta this week), std::sync::Mutex, RwLock, and Condvar no longer do any allocations on Linux. πŸŽ‰

Benchmarking locks is extremely tricky, as their performance depends heavily on the exact use case, but there are very noticable differences: A table showing before and after times of three tests.  test
std's Mutex basically used to contain a Pin<Box<pthread_mutex_t>>, where the pinned Box was only necessary because pthread_mutex_t is not guaranteed movable. The new Mutex no longer uses pthread, and instead directly uses the futex syscall, making it smaller and more efficient.
Also, the new RwLock on Linux prefers writers, which prevents writer starvation. pthread_rwlock_t prefers readers by default, to allow recursive read locking. Rust's RwLock does not make recursion guarantees, and on several platforms (including Windows) already preferred writers.
Read 4 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

Don't want to be a Premium member but still want to support us?

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

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(