Live data from Hacker News

Rust 1.26 released

blog.rust-lang.org

11–20 of 178 posts

Re: Rust 1.26 released

#11
post #4

Earlier quoted context omitted.

impl Trait and the second edition of the book; very nice! Congrats on the release! Question: Is there any way to preview what the book's typesetting looks like, since there's praise in the release note?

I am not sure to be honest, I think NoStarch has a preview chapter on the page for the book, which would show it off.

You're right, I totally missed that link. Thank you!

It shows off Chapter 3, and yes, it really looks great.

Re: Rust 1.26 released

#12

So, so, so much stuff in this release! The next few are shaping up to be similar. Very exciting times! As always, happy to answer questions, provide context, etc.

In this example:

  fn foo(x: i32) -> Box> {
      let iter = vec![1, 2, 3]
          .into_iter()
          .map(|x| x + 1);

      if x % 2 == 0 {
          Box::new(iter.filter(|x| x % 2 == 0))
      } else {
          Box::new(iter)
      }
  }
Why is it that I can't return `impl Iterator`? Doesn't the `Filter` type implement `Iterator` for the same associated type?

Re: Rust 1.26 released

#13
Everyone keeps talking about impl Trait (which is great) but I'm super pumped for ? working in main now. Was recently writing some code as I finally got back to rust and forgot about that edge and had to write a match block when ? would have been good enough (felt silly to make a method just to handle that).

Re: Rust 1.26 released

#14

So, so, so much stuff in this release! The next few are shaping up to be similar. Very exciting times! As always, happy to answer questions, provide context, etc.

In this example: fn foo(x: i32) -> Box > { let iter = vec![1, 2, 3] .into_iter() .map(|x| x + 1); if x % 2 == 0 { Box::new(iter.filter(|x| x % 2 == 0)) } else { Box::new(iter) } } Why is it that I can't return `impl Iterator `? Doesn't the `Filter` type implement `Iterator` for the same associated type?

The guarantee of `impl Trait` is that if I want to call a trait method on the returned object, e.g. for iterators if I want to call `foo(0).next()`, then the function pointer will always be in the same place on the object in memory (static dispatch).

By contrast, if I returned a boxed trait, then calling the trait method requires a dynamic lookup to find the method on the boxed object, and then jumping to that function (dynamic dispatch). In this example, `iter.map(..)` and `iter.filter(..)` return two different implementations of the Iterator trait, so dynamic dispatch is required.

In general, if your function returns multiple possible implementations of a given trait, then the compiler cannot know where the trait methods will be statically, so it is impossible to do static dispatch. Since impl Trait wants to guarantee static dispatch, it requires that only one possible implementation of the trait be returned.

Re: Rust 1.26 released

#15
"Speaking of print, you can pre-order a dead tree version of the book from NoStarch Press. The contents are identical, but you get a nice physical book to put on a shelf, or a beautifully typeset PDF. Proceeds are going to charity."

Which charity/charities?

Re: Rust 1.26 released

#16

So, so, so much stuff in this release! The next few are shaping up to be similar. Very exciting times! As always, happy to answer questions, provide context, etc.

In this example: fn foo(x: i32) -> Box > { let iter = vec![1, 2, 3] .into_iter() .map(|x| x + 1); if x % 2 == 0 { Box::new(iter.filter(|x| x % 2 == 0)) } else { Box::new(iter) } } Why is it that I can't return `impl Iterator `? Doesn't the `Filter` type implement `Iterator` for the same associated type?

I think because returning two different types requires dynamic dispatch when using the returned objects, which requires Box; if the function only returns one type then that type can be determined at runtime and further function calls can be implemented with static dispatch instead.

Re: Rust 1.26 released

#17

So, so, so much stuff in this release! The next few are shaping up to be similar. Very exciting times! As always, happy to answer questions, provide context, etc.

In this example: fn foo(x: i32) -> Box > { let iter = vec![1, 2, 3] .into_iter() .map(|x| x + 1); if x % 2 == 0 { Box::new(iter.filter(|x| x % 2 == 0)) } else { Box::new(iter) } } Why is it that I can't return `impl Iterator `? Doesn't the `Filter` type implement `Iterator` for the same associated type?

The mismatch isn’t about the associated type, it’s about the actual, underlying type. One is a Map and one is a Filter. It’s not possible to determine which is returned, so you inherently need dynamic dispatch.

There is some discussion about it possibly being sugar for an anonymous enum in the future, but that’s not what it is right now.

Re: Rust 1.26 released

#18
So freaking excited about impl trait.

This and not being able to do paramterized array sizes for things like SoA, AoSoA where the two things that I feel like were missing from Rust. Really happy to see the first landing(and I understand work is going on for the second).

Re: Rust 1.26 released

#19
post #15

"Speaking of print, you can pre-order a dead tree version of the book from NoStarch Press. The contents are identical, but you get a nice physical book to put on a shelf, or a beautifully typeset PDF. Proceeds are going to charity." Which charity/charities?

Black Girls Code is the intention today, but originally it was going to be a different tech charity that is no longer around, OpenHatch.

Re: Rust 1.26 released

#20

Earlier quoted context omitted.

In this example: fn foo(x: i32) -> Box > { let iter = vec![1, 2, 3] .into_iter() .map(|x| x + 1); if x % 2 == 0 { Box::new(iter.filter(|x| x % 2 == 0)) } else { Box::new(iter) } } Why is it that I can't return `impl Iterator `? Doesn't the `Filter` type implement `Iterator` for the same associated type?

The mismatch isn’t about the associated type, it’s about the actual, underlying type. One is a Map and one is a Filter. It’s not possible to determine which is returned, so you inherently need dynamic dispatch. There is some discussion about it possibly being sugar for an anonymous enum in the future, but that’s not what it is right now.

Is there an RFC for the anonymous enum thing? I would gladly write the implementation ... :D
Post reply on HN