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
- Clarify that `: TYPE` is allowed, as per Josh
- Note the expression restriction more accurately and with more detail, as per Niko
- Removed the option-chaining example, because many people keep stopping at that to comment.
- Note a macro as an (unfavorable) alternative.
// RFC comment: ok_or_else works fine to early return when working with `Option`.
119
93
.ok_or_else(||eyre::eyre!("Entity has no history"))?;
120
94
121
95
letAction::Register {
@@ -124,9 +98,7 @@ impl ActionView {
124
98
y:u32,
125
99
z:String,
126
100
} =event.action().clone() else {
127
-
// RFC Author's note:
128
-
// Without if-else this was separated from the conditional
129
-
// by a substantial block of code which now follows below.
101
+
// RFC comment: Directly located next to the associated conditional.
130
102
returnErr(eyre::eyre!("must begin with a Register action"));
131
103
};
132
104
@@ -145,6 +117,9 @@ impl ActionView {
145
117
foreventiniter {
146
118
view.update(&event)?;
147
119
}
120
+
121
+
// more lines omitted
122
+
148
123
Ok(view)
149
124
}
150
125
}
@@ -233,12 +208,17 @@ let (each, binding) = match expr {
233
208
};
234
209
```
235
210
236
-
Any expression may be put into the expression position except an `if {} else {}` as explain below in [drawbacks][].
237
-
While `if {} else {}` is technically feasible this RFC proposes it be disallowed for programmer clarity to avoid an `... else {} else {}` situation.
238
-
Rust already provides us with such a restriction, [`ExpressionWithoutBlock`][expressions].
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].)
213
+
2. May not be just a lazy boolean expression (`&&` or `||`). (Must not be a [`LazyBooleanExpression`][lazy-boolean-operators].)
239
214
240
-
Any pattern that could be put into if-let's pattern position can be put into let-else's pattern position, except a match to a boolean when
241
-
a lazy boolean operator is present (`&&` or `||`), for reasons noted in [future-possibilities][].
215
+
While allowing e.g. `if {} else {}` directly in the expression position is technically feasible this RFC proposes it be
216
+
disallowed for programmer clarity so as to avoid `... else {} else {}` situations as discussed in the [drawbacks][] section.
217
+
Boolean matches are not useful with let-else and so lazy boolean expressions are disallowed for reasons noted in [future-possibilities][].
218
+
These types of expressions can still be used when combined in a less ambiguous manner with parenthesis, thus forming a [`GroupedExpression`][grouped-expr],
219
+
which is allowed under the two expression restrictions.
220
+
221
+
Any refutable pattern that could be put into if-let's pattern position can be put into let-else's pattern position.
242
222
243
223
If the pattern is irrefutable, rustc will emit the `irrefutable_let_patterns` warning lint, as it does with an irrefutable pattern in an `if let`.
244
224
@@ -474,6 +454,13 @@ let z = match x {
474
454
475
455
This is, as stated, a much more complex alternative interacting with much more of the language, and is also not an obvious opposite of if-let expressions.
476
456
457
+
### Macro
458
+
459
+
Another suggested solution is to create a macro which handles this.
460
+
A crate containing such a macro is mentioned in the [Prior art](prior-art) section of this RFC.
461
+
462
+
This crate has not been widely used in the rust crate ecosystem with only 47k downloads over the ~6 years it has existed at the time of writing.
463
+
477
464
### Null Alternative
478
465
479
466
Don't make any changes; use existing syntax like `match` (or `if let`) as shown in the motivating example, or write macros to simplify the code.
@@ -623,10 +610,12 @@ because it is confusing to read syntactically, and it is functionally similar to
0 commit comments