Other Usage Scenarios of Patterns
Patterns can be used not only in match expressions but also in variable definitions and for in expressions. For example, the left side of an equals sign is a pattern, and the part between the for keyword and the in keyword is also a pattern. Additionally, conditions in if expressions and while expressions can utilize patterns. For specific examples, refer to the “Conditions Involving let-pattern” section.
However, not all patterns can be used in variable definitions and for in expressions. Only irrefutable patterns are permitted in these contexts. Therefore, only wildcard patterns, binding patterns, irrefutable tuple patterns, and irrefutable enum patterns are allowed.
-
Examples of using wildcard patterns in variable definitions and
for inexpressions:main() { let _ = 100 for (_ in 1..5) { println("0") } }In the above example, a wildcard pattern is used in the variable definition, indicating the creation of a nameless variable (which consequently cannot be accessed later). The
for inexpression uses a wildcard pattern, meaning elements from1..5won’t be bound to any variable (thus their values cannot be accessed within the loop body). Compiling and executing this code yields:0 0 0 0 -
Examples of using binding patterns in variable definitions and
for inexpressions:main() { let x = 100 println("x = ${x}") for (i in 1..5) { println(i) } }Here,
xin the variable definition andiin thefor inexpression are both binding patterns. Compiling and executing this code yields:x = 100 1 2 3 4 -
Examples of using
irrefutabletuple patterns in variable definitions andfor inexpressions:main() { let (x, y) = (100, 200) println("x = ${x}") println("y = ${y}") for ((i, j) in [(1, 2), (3, 4), (5, 6)]) { println("Sum = ${i + j}") } }In this example, a tuple pattern is used in the variable definition to destructure
(100, 200)and bind its components toxandy, effectively defining two variables. Thefor inexpression employs a tuple pattern to sequentially extract tuple-type elements from[(1, 2), (3, 4), (5, 6)], destructure them, and bind their components toiandj, then output their sum in the loop body. Compiling and executing this code yields:x = 100 y = 200 Sum = 3 Sum = 7 Sum = 11 -
Examples of using
irrefutableenum patterns in variable definitions andfor inexpressions:enum RedColor { Red(Int64) } main() { let Red(red) = Red(0) println("red = ${red}") for (Red(r) in [Red(10), Red(20), Red(30)]) { println("r = ${r}") } }Here, an enum pattern is used in the variable definition to destructure
Red(0)and bind its constructor parameter (i.e.,0) tored. Thefor inexpression uses an enum pattern to sequentially extract elements from[Red(10), Red(20), Red(30)], destructure them, and bind their constructor parameters tor, then outputrin the loop body. Compiling and executing this code yields:red = 0 r = 10 r = 20 r = 30