模式 Patterns

模式类别例子备注
Literal100, "name"Matches an exact value; the name of a const is also allowed
Range0...100, 'a'...'k'Matches any value in range, including the end value
Wildcard_Matches any value and ignores it
Variablename, mut countLike _ but moves or copies the value into a new local variable
ref variableref field, ref mut fieldBorrows a reference to the matched value instead of moving or coping it
Reference&value, &(k, v)Matches only reference values
Binding with subpatternval @ 0...99, ref circle @Shape::Circle {...}Matches the pattern to the right of @, using the variable name to the left
Enum patternSome(val), None
Tuple pattern(key, value), (r, g, b)
Struct patternColor(r, g, b), Point{x, y}
Multiple patterns`'a''k'`
Guard expressionx if x * x <= r2In match only (not valid in let, etc.)