Mara Bos Profile picture
24 Apr, 6 tweets, 4 min read
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
So instead, we wanted to attach this to an edition change. However, you can't just have a trait implementation exist in one edition and not in another, since editions can be mixed. @nikomatsakis came up with an alternative idea:

4/6 Proposal summary  Allow traits to be annotated with #[rustc_
We would add the trait implementation for all editions, but with small hack to make `array.into_iter()` syntax ignore that impl until the next edition. For all other syntax like for loops, methods requiring IntoIterator, etc, arrays will suddenly work on all editions!

5/6 for e in [1, 2, 3] {} // Ok!  some_vec.extend([1, 2, 3]); //
Just a few hours later, @cuviper already managed to implement it. Both the language and library teams were pretty excited and approved of the change. No major concerns came up in the last 10 days (the 'final comment period'), which means it's now being merged into Rust 1.53!

6/6 Cautiously add IntoIterator for arrays by value #84147  cuvibors commented 1 minute ago  Commit 2a97c69 has been approve

• • •

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

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

Thread:
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,...pub 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!