book
book copied to clipboard
Addition of `let else` expression
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.
Can you give me some more information regarding this ?
Well, the expression let-else is not included in the book, will it be added to the following version of the book?
@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
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.
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?
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
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!