Skip to content

Commit e8c51c0

Browse files
committed
add future possibility of allowing captured bindings
1 parent 50157c7 commit e8c51c0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

text/3637-guard-patterns.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,28 @@ One way to think about this is that patterns serve two functions:
286286
Guard patterns as described here provide _arbitrary refinement_. That is, guard patterns can match based on whether any arbitrary expression evaluates to true.
287287

288288
Allowing `if let` allows not just arbitrary refinement, but also _arbitrary destructuring_. The value(s) bound by an `if let` pattern can depend on the value of an arbitrary expression.
289+
290+
## Allowing Mismatching Bindings When Possible
291+
292+
Users will likely want to write something like
293+
294+
```rust
295+
match Some(0) {
296+
Some(x if x > 0) | None => {},
297+
_ => {}
298+
}
299+
```
300+
301+
As mentioned above, this case is not covered by this RFC, because `x` would need to be bound in both cases of the disjunction.
302+
303+
However, we could support this by automatically detecting that `x` is not ever used outside of the guard pattern, and allowing the guard to capture the binding, so it wouldn't have to be bound in other cases of the disjunction.
304+
305+
We could also make this capturing behavior explicit, with some kind of syntax extending guard patterns:
306+
307+
```rust
308+
// example syntax by analogy with closures
309+
// probably not what we'd want to go with, since you can't specify which bindings are captured
310+
Some(x move if x > 0) | None
311+
```
312+
313+
This would also give the guard ownership of the bound value, which may be desirable in other cases.

0 commit comments

Comments
 (0)