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

Apr 24, 2021, 6 tweets

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

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

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

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

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

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

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