-
Notifications
You must be signed in to change notification settings - Fork 540
if let
guards documentation
#1823
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,15 +9,25 @@ MatchExpression -> | |||||
MatchArms? | ||||||
`}` | ||||||
|
||||||
Scrutinee -> Expression _except [StructExpression]_ | ||||||
Scrutinee -> | ||||||
Expression _except_ [StructExpression] | ||||||
|
||||||
MatchArms -> | ||||||
( MatchArm `=>` ( ExpressionWithoutBlock `,` | ExpressionWithBlock `,`? ) )* | ||||||
MatchArm `=>` Expression `,`? | ||||||
|
||||||
MatchArm -> OuterAttribute* Pattern MatchArmGuard? | ||||||
MatchArm -> | ||||||
OuterAttribute* Pattern MatchArmGuard? | ||||||
|
||||||
MatchArmGuard -> `if` Expression | ||||||
MatchArmGuard -> | ||||||
`if` MatchConditions | ||||||
|
||||||
MatchConditions -> | ||||||
MatchCondition ( `&&` MatchCondition )* | ||||||
|
||||||
MatchCondition -> | ||||||
OuterAttribute* `let` Pattern `=` Scrutinee | ||||||
| Expression | ||||||
``` | ||||||
<!-- TODO: The exception above isn't accurate, see https://github.com/rust-lang/reference/issues/569 --> | ||||||
|
||||||
|
@@ -150,6 +160,93 @@ This allows shared borrows to be used inside guards without moving out of the sc | |||||
r[expr.match.guard.no-mutation] | ||||||
Moreover, by holding a shared reference while evaluating the guard, mutation inside guards is also prevented. | ||||||
|
||||||
r[expr.match.if.let.guard] | ||||||
## If Let Guards | ||||||
Match arms can include `if let` guards to allow conditional pattern matching within the guard clause. | ||||||
|
||||||
r[expr.match.if.let.guard.syntax] | ||||||
```rust,ignore | ||||||
match expression { | ||||||
pattern if let subpattern = guard_expr => arm_body, | ||||||
... | ||||||
} | ||||||
``` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to make a working example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im not really sure, because it's not stable feature and contains behind
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just add a full example that will fail CI for now. |
||||||
Here, `guard_expr` is evaluated and matched against `subpattern`. If the `if let` expression in the guard matches successfully, the arm's body is executed. Otherwise, pattern matching continues to the next arm. | ||||||
|
||||||
r[expr.match.if.let.guard.behavior] | ||||||
When the pattern matches successfully, the `if let` expression in the guard is evaluated: | ||||||
* The guard proceeds if the inner pattern (`subpattern`) matches the result of `guard_expr`. | ||||||
* Otherwise, the next arm is tested. | ||||||
|
||||||
```rust,ignore | ||||||
let value = Some(10); | ||||||
|
||||||
let msg = match value { | ||||||
Some(x) if let Some(y) = Some(x - 1) => format!("Matched inner value: {}", y), | ||||||
_ => "No match".to_string(), | ||||||
}; | ||||||
``` | ||||||
|
||||||
r[expr.match.if.let.guard.scope] | ||||||
* The `if let` guard may refer to variables bound by the outer match pattern. | ||||||
* New variables bound inside the `if let` guard (e.g., `y` in the example above) are available within the body of the match arm where the guard evaluates to `true`, but are not accessible in other arms or outside the match expression. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be worth being a bit more precise w.r.t drop behavior (i.e. that anything bound in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, we can add something like this
What's your opinion on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drop behavior should be documented in the destructors chapter. I would expect it to be somewhat similar to the changes that were made for The stabilization report doesn't explain what the drop order is or how temporaries are handled, so it is difficult for me to evaluate what this should say. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like this? I'm just not sure how full should be explanation about drop order
|
||||||
|
||||||
```rust,ignore | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
let opt = Some(42); | ||||||
|
||||||
match opt { | ||||||
Some(x) if let Some(y) = Some(x + 1) => { | ||||||
// Both `x` and `y` are available in this arm, | ||||||
// since the pattern matched and the guard evaluated to true. | ||||||
println!("x = {}, y = {}", x, y); | ||||||
} | ||||||
_ => { | ||||||
// `y` is not available here --- it was only bound inside the guard above. | ||||||
// Uncommenting the line below will cause a compile-time error: | ||||||
// println!("{}", y); // error: cannot find value `y` in this scope | ||||||
} | ||||||
} | ||||||
|
||||||
// Outside the match expression, neither `x` nor `y` are in scope. | ||||||
``` | ||||||
|
||||||
* The outer pattern variables (`x`) follow the same borrowing behavior as in standard match guards (see below). | ||||||
|
||||||
r[expr.match.if.let.guard.drop] | ||||||
|
||||||
* Variables bound inside `if let` guards are dropped before evaluating subsequent match arms. | ||||||
|
||||||
* Temporaries created during guard evaluation follow standard drop semantics and are cleaned up appropriately. | ||||||
|
||||||
r[expr.match.if.let.guard.borrowing] | ||||||
Variables bound by the outer pattern follow the same borrowing rules as standard match guards: | ||||||
* A shared reference is taken to pattern variables before guard evaluation | ||||||
* Values are moved or copied only when the guard succeeds | ||||||
* Moving from outer pattern variables within the guard is restricted | ||||||
|
||||||
```rust,ignore | ||||||
fn take<T>(value: T) -> Option<T> { Some(value) } | ||||||
|
||||||
let val = Some(vec![1, 2, 3]); | ||||||
match val { | ||||||
Some(v) if let Some(_) = take(v) => "moved", // ERROR: cannot move `v` | ||||||
Some(v) if let Some(_) = take(v.clone()) => "cloned", // OK | ||||||
_ => "none", | ||||||
} | ||||||
``` | ||||||
> [!NOTE] | ||||||
> Multiple matches using the `|` operator can cause the pattern guard and the side effects it has to execute multiple times. For example: | ||||||
> ```rust,ignore | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
> use std::cell::Cell; | ||||||
> | ||||||
> let i: Cell<i32> = Cell::new(0); | ||||||
> match 1 { | ||||||
> 1 | _ if let Some(_) = { i.set(i.get() + 1); Some(1) } => {} | ||||||
> _ => {} | ||||||
> } | ||||||
> assert_eq!(i.get(), 2); // Guard is executed twice | ||||||
> ``` | ||||||
|
||||||
r[expr.match.attributes] | ||||||
## Attributes on match arms | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be missing some things.
Similar to
if
expressions, this will need two separate rules. Something likeExpression | MatchConditions
because without alet
it is allowed to have LazyBooleanExpression. Then,MatchConditions
will need to exclude LazyBooleanExpression.I don't think this should use Scrutinee, because that rejects StructExpression, but that is allowed here.
This will also need similar precedence restrictions as
if let
has with restrictingLazyBooleanExpression | RangeExpr | RangeFromExpr | RangeInclusiveExpr | AssignmentExpression | CompoundAssignmentExpression
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess, something simple like this should work right? I just not sure if I can reuse LetChain like this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because LetChain excludes StructExpression, but we don't want to exclude that here.
We also don't want MatchConditions to exclude StructExpression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it's little bit hard to write a working syntax to cover all cases, let's try something like this as a start
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there needs to be a separate case for a chain vs not-chain.
For example:
so I think what it needs is something like:
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, yeah, separate
MatchGuardScrutinee
is a good idea