book icon indicating copy to clipboard operation
book copied to clipboard

Addition of `let else` expression

Open Phosphorus-M opened this issue 2 years ago • 7 comments
trafficstars

Hi! I'm translating the book and note that the book doesn't show anything about expressions like let else. Have you some plans to update the book to include this feature? To me, it's a great implementation of the control flow using let else.

Phosphorus-M avatar May 29 '23 03:05 Phosphorus-M

Can you give me some more information regarding this ?

postmeback avatar Jul 08 '23 09:07 postmeback

Well, the expression let-else is not included in the book, will it be added to the following version of the book?

Phosphorus-M avatar Jul 10 '23 04:07 Phosphorus-M

@postmeback: Can you give me some more information regarding this ?

Examples:

  • https://doc.rust-lang.org/rust-by-example/flow_control/let_else.html
  • https://rust-lang.github.io/rfcs/3137-let-else.html

atc0005 avatar Jul 10 '23 10:07 atc0005

Let-else is a very useful construct. It makes the code flow more linear with less indentation when only a particular enum variant is expected, yet it allows for adequate error handling if the expectation doesn't hold.

It's a relatively new construct, stabilized in Rust 1.65. Formatting support for let-else appeared in nightly earlier this month, the stable rustfmt would just keep the construct as is, but I hope it would change soon.

I hope this book doesn't just document let-else, but starts using it wherever appropriate.

proski avatar Jul 21 '23 23:07 proski

I hope this book doesn't just document let-else, but starts using it wherever appropriate.

Yeah, I agree with you, it's too helpful and the first time I didn't understand it, today it's one of my favorite things of Rust. Maybe do a comparative with the let-if?

Phosphorus-M avatar Jul 25 '23 16:07 Phosphorus-M

In fact, let else is already in the book as part of a compiler suggestion

help: you might want to use `if let` to ignore the variant that isn't matched
  |
3 |     let x = if let Some(x) = some_option_value { x } else { todo!() };
  |     ++++++++++                                 ++++++++++++++++++++++
help: alternatively, you might want to use let else to handle the variant that isn't matched
  |
3 |     let Some(x) = some_option_value else { todo!() };
  |                                     ++++++++++++++++

See https://doc.rust-lang.org/book/ch18-02-refutability.html

proski avatar Aug 04 '23 20:08 proski

Great call-out here. I definitely think we should get a mention of this somewhere in the text! We will have to figure out where the best spot to introduce it is, and we will also want to evaluate how much we want to change existing examples. No doubt there are a bunch of places that could be updated to this, but we also do not necessarily want to introduce a ton of changes just because we can.

That said, if someone felt like compiling a list of all the places the text would benefit from this, I wouldn’t complain. 😂 Otherwise, that’s probably the kind of thing I will get to later this month!

chriskrycho avatar Apr 01 '24 20:04 chriskrycho