You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hopefully this will be my last round of updates.
- Added some unresolved questions.
- Fixed some typos / english language clarification.
- Clarified that all expressions ending in `}` should be disallowed.
- Fixed some errors in examples.
- Added section on `, else`.
- Updated the `unless let` section to be "Introducer syntax" with the noted keyword being `guard`.
- Noted the `DIVERGING_EXPR` section with more detail.
@@ -209,7 +209,9 @@ let (each, binding) = match expr {
209
209
```
210
210
211
211
Most expressions may be put into the expression position with two restrictions:
212
-
1. May not include a block outside of parenthesis. (Must be an [`ExpressionWithoutBlock`][expressions].)
212
+
1. May not include a block outside of parenthesis.
213
+
- Must be an [`ExpressionWithoutBlock`][expressions].
214
+
-[`GroupedExpression`][grouped-expr]-s ending with a `}` are additionally not allowed and must be put in parenthesis.
213
215
2. May not be just a lazy boolean expression (`&&` or `||`). (Must not be a [`LazyBooleanExpression`][lazy-boolean-operators].)
214
216
215
217
While allowing e.g. `if {} else {}` directly in the expression position is technically feasible this RFC proposes it be
@@ -231,7 +233,7 @@ accessible as they would normally be.
231
233
For patterns which match multiple variants, such as through the `|` (or) syntax, all variants must produce the same bindings (ignoring additional bindings in uneven patterns),
232
234
and those bindings must all be names the same. Valid example:
let-else does not combine with the `let` from if-let, as if-let is not actually a _let statement_.
@@ -247,7 +249,7 @@ Desugars to
247
249
248
250
```rust
249
251
letx=matchy {
250
-
Some(x) =>y,
252
+
Some(x) =>x,
251
253
_=> {
252
254
letnope:!= { return; };
253
255
matchnope {}
@@ -263,7 +265,7 @@ let x = match y {
263
265
"Must diverge" is an unusual requirement, which doesn't exist elsewhere in the language as of the time of writing,
264
266
and might be difficult to explain or lead to confusing errors for programmers new to this feature.
265
267
266
-
However, rustc does have support for representing the divergence through the type-checker via `!` or any other uninhabitable type,
268
+
However, rustc does have support for representing the divergence through the type-checker via `!` or any other uninhabited type,
267
269
so the implementation is not a problem.
268
270
269
271
## `let PATTERN = if {} else {} else {};`
@@ -322,9 +324,17 @@ This is supposed to help disambiguate let-else statements from other code with b
322
324
This RFC avoids this as it would mean losing symmetry with if-else and if-let-else, and would require adding a new keyword.
323
325
Adding a new keyword could mean more to teach and could promote even more special casing around let-else's semantics.
324
326
325
-
### `unless let ... {}` / `try let ... {}`
327
+
### Comma-before-else (`, else { ... }`)
326
328
327
-
Another often proposed alternative is to add an extra keyword to the beginning of the let-else statement, to denote that it is different than a regular `let` statement.
329
+
Another proposal very similar to renaming `else` it to have it be proceeded by some character such as a comma.
330
+
331
+
It is possible that adding such additional separating syntax would make combinations with expressions which have blocks
332
+
easier to read and less ambiguous, but is also generally inconsistent with the rest of the rust language at time of writing.
333
+
334
+
### Introducer syntax (`guard let ... {}`)
335
+
336
+
Another often proposed alternative is to add some introducer syntax (usually an extra keyword) to the beginning of the let-else statement,
337
+
to denote that it is different than a regular `let` statement.
328
338
329
339
One possible benefit of adding a keyword is that it could make a possible future extension for similarity to the (yet unimplemented) [if-let-chains][] feature more straightforward.
330
340
However, as mentioned in the [future-possibilities][] section, this is likely not necessary.
@@ -348,17 +358,18 @@ and partway through that RFC's lifecycle it was updated to be similar to this RF
348
358
349
359
The `if !let` alternative syntax would also share the binding drawback of the `unless let` alternative syntax.
350
360
351
-
### `let PATTERN = EXPR else return EXPR;`
361
+
### `let PATTERN = EXPR else DIVERGING_EXPR;`
352
362
353
-
A potential alternative to requiring parentheses in `let PATTERN = (if { a } else { b }) else { c };` is to change the syntax of the `else` to no longer be a block
354
-
but instead an expression which starts with a diverging keyword, such as `return` or `break`.
363
+
A potential alternative to requiring parentheses in `let PATTERN = (if { a } else { b }) else { c };`
364
+
is to change the syntax of the `else` to no longer be a block but instead _any_ expression which diverges,
365
+
such as a `return`, `break`, or any block which diverges.
355
366
356
367
Example:
357
-
```
368
+
```rust
358
369
letSome(foo) =some_optionelsereturnNone;
359
370
```
360
371
361
-
This RFC avoids this because it is overall less consistent with `else` from if-else, which require blocks.
372
+
This RFC avoids this because it is overall less consistent with `else` from if-else, which requires block expressions.
362
373
363
374
This was originally suggested in the old RFC, comment at https://github.com/rust-lang/rfcs/pull/1303#issuecomment-188526691
364
375
@@ -409,7 +420,7 @@ match thing {
409
420
}
410
421
```
411
422
412
-
However this is not an obvious opposite io if-let, and would introduce an entirely new positional meaning of `let`.
423
+
However this is not an obvious opposite to if-let, and would introduce an entirely new positional meaning of `let`.
413
424
414
425
### `||` in pattern-matching
415
426
@@ -430,13 +441,13 @@ let Some(x) = a || b || { return; };
430
441
Combined with `&&` as proposed in if-let-chains, constructs such as the following are conceivable:
0 commit comments