Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of let else expression #3662

Closed
Phosphorus-M opened this issue May 29, 2023 · 9 comments
Closed

Addition of let else expression #3662

Phosphorus-M opened this issue May 29, 2023 · 9 comments

Comments

@Phosphorus-M
Copy link

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 Phosphorus-M changed the title Update of the book Addition of let else expression May 29, 2023
@postmeback
Copy link

Can you give me some more information regarding this ?

@Phosphorus-M
Copy link
Author

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

@atc0005
Copy link

atc0005 commented Jul 10, 2023

@proski
Copy link
Contributor

proski commented Jul 21, 2023

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.

@Phosphorus-M
Copy link
Author

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?

@proski
Copy link
Contributor

proski commented Aug 4, 2023

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

@chriskrycho
Copy link
Contributor

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!

@LorenzoBloedow
Copy link

Hi @chriskrycho, I think I found a good spot to introduce let-else, look at the code block in this section.
I'm open to writing a PR to change the code if you'd like :)

@chriskrycho
Copy link
Contributor

@LorenzoBloedow ah, as you’ll see in the version on beta and nightly, you’ll see that I actually introduced let...else in 6.3, in #3702. Looks like we can close this issue now! Thanks for the suggestion, though, and thanks to other folks on this thread for flagging up the gap!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants