[Video] What's new in Rust 2021?

Hey all, I made a short video covering what's new in Rust 2021! I'm thinking of making more like these in the future; let me know what you think or what topics you might want me to cover. Thanks!

All of your code examples around arrays and IntoIterator are inaccurate/wrong

1:26

// this now works in Rust 2021!
for num in [1, 2, 3] {
    println!("num: {}", num);
}

Works in 2018 edition, too. And in 2015 edition as well. Works since 1.53.0 (released June 17, 2021).


1:31

// This was the only way to iterate
// arrays directly in Rust 2018
// (introduced in Rust 1.56)

let arr = [1, 2, 3];
for item in std::array::IntoIter::new(arr) {
    println!("{}", item);
}

Nope. Introduced in 1.51.0 (Mar. 25, 2021); and since 1.53.0 it’s no longer the only way, from then on you can (in any edition, including Rust 2018 or Rust 2015) also write

let arr = [1, 2, 3];
for item in IntoIterator::into_iter(arr) {
    println!("{}", item);
}

or

let arr = [1, 2, 3];
for item in <_>::into_iter(arr) {
    println!("{}", item);
}

or

let arr = [1, 2, 3];
for item in arr {
    println!("{}", item);
}

1:38

// Only in Rust 2021
let map = HashMap::from([
    (1, "chashu"),
    (2, "nori")
]);

Again, incorrect. It works in Rust 2015 and 2018, too, since 1.53.0.


In general you do a bad job at explaining editions IMO. Editions aren’t a huge thing and about breaking changes only, as you mentioned, but then you fail to point out for any of the presented changes (disjoint captures, new traits in prelude, .into_iter() method resolution) in what way they are breaking changes. (Disjoint captures can (among other things) influence drop order, traits in the prelude can cause ambiguities in method resolution, calling .into_iter() (method syntax) on arrays used to produce a by-reference iterator equivalent to calling .iter()).

Hey @steffahn, I appreciate the feedback. You're right that I should've been more thorough in gathering examples for IntoIterator for Array. I misinterpreted the nuance of the change as presented in the edition guide, and missed the trait was introduced last June in 1.53, not 1.56.

I've added a correction in the video comments, which will hopefully clear up any confusion folks might have. Thanks!

4 Likes

Right, so for context

  • The type array::IntoIter was introduced in 1.51, with its constructor array::IntoIter::new
  • The implementation IntoIterator for [T; N] { type IntoIter = array::IntoIter; type Item = T; … } was introduced with 1.53, together with special rules for method resolution so that method syntax x.into_iter() on x: [T; N] still resolves to (&x).into_iter(), yet any other thing that relies on [T; N]: IntoIterator works from then on in all editions, including for x in [1, 2, 3] or IntoIter::into_iter(array) or HashMap::from([(k, v), …])
  • The edition 2021 was introduced in 1.56, activating edition 2021 disables the special rule for method-resolution of x.into_iter()

Everything else in the video seems mostly correct; maybe introducing disjoint captures as a change to the borrow-checker isn’t quite accurate, but understandable as from a user’s perspective it does somewhat feel like one. As said, would’ve loved some mention of what exactly is breaking about the mentioned changes, and personally I’d probably include the changes to panic!(…) in a short list of noteworthy changes, but you also link the official blog posts for more details, so people can find out all the details anyway if they want.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.