Skip to content

Commit a84f01b

Browse files
committed
further explain behavior localization example
1 parent 64a234a commit a84f01b

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

text/3637-guard-patterns.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,18 @@ For example, consider a function that iterates over a list of customer orders an
179179
```rust
180180
match order {
181181
Order {
182+
// These patterns match based on method calls, necessitating the use of a guard pattern:
182183
customer: customer if customer.subscription_plan() == Plan::Premium,
183184
payment: Payment::Cash(amount) if amount.in_usd() > 100,
185+
184186
item_type: ItemType::A,
185187
// a bunch of other conditions...
186188
} => { /* ... */ }
187189
// other similar branches...
188190
}
189191
```
190192

191-
Here, the pattern `customer if customer.subscription_plan() == Plan::Premium` has a clear meaning: it matches customers with premium subscriptions. All of the behavior of the pattern pertaining to the customer is in one place. However, if we move the guard outwards to wrap the entire order, the behavior is spread out and much harder to understand -- particularly if it is merged with conditions for other parts of the order struct:
193+
Here, the pattern `customer if customer.subscription_plan() == Plan::Premium` has a clear meaning: it matches customers with premium subscriptions. Similarly, `Payment::Cash(amount) if amount.in_usd() > 100` matches cash payments of amounts greater than 100USD. All of the behavior of the pattern pertaining to the customer is in one place, and all behavior pertaining to the payment is in another. However, if we move the guard outwards to wrap the entire order struct, the behavior is spread out and much harder to understand -- particularly if the two conditions are merged into one:
192194

193195
```rust
194196
// The same match statement using or-of-guards.

0 commit comments

Comments
 (0)