5. Patterns¶
Syntax
Pattern
::= |?PatternWithoutAlternation
(|PatternWithoutAlternation
)*PatternList
::=Pattern
(,Pattern
)* ,?PatternWithoutAlternation
::=PatternWithoutRange
|RangePattern
PatternWithoutRange
::=IdentifierPattern
|LiteralPattern
|MacroInvocation
|ParenthesizedPattern
|PathPattern
|ReferencePattern
|RestPattern
|SlicePattern
|StructPattern
|TuplePattern
|UnderscorePattern
Legality Rules
5:1 A pattern is a construct that matches a value which satisfies all the criteria of the pattern.
5:2 A pattern-without-alternation is a pattern that cannot be alternated.
5:3 A pattern-without-range is a pattern-without-alternation that excludes range patterns.
5:4 A subpattern is a pattern nested within another pattern.
5:5
A pattern has a type, with the exception of the rest pattern if
it is not the inner pattern of a slice pattern or the pattern of
a possibly nested identifier pattern of a slice pattern.
5:6 The expected type of a pattern is the type the pattern is being matched against.
5:7
It is a static error when lexical elements match multiple alternations
of a pattern-without-range, except for when the pattern is &mut
Identifier
. Such a pattern is interpreted as a reference pattern
with keyword mut
containing an identifier pattern.
5:8 Any two pattern-without-alternations that are or-ed using character 0x7C (vertical line) are subject to the following restrictions:
5:9 The types of the two pattern-without-alternations shall be unifiable.
5:10 The bindings of the two pattern-without-alternations shall be the same, shall have unifiable types, and shall have the same binding modes.
5.1. Refutability¶
Legality Rules
5.1:1 Refutability is a property of patterns that expresses the ability to match all possible values of a type.
5.1:2 An irrefutable pattern is a pattern that always matches any value of its type.
5.1:3 A refutable pattern is a pattern that has a possibility of not matching a value of its type
5.1:4 A pattern that is not an irrefutable pattern is a refutable pattern.
5.1:5 An irrefutable constant is a constant of a type that has at most one value.
5.1:6 A refutable constant is a constant of a refutable type.
Examples
5.1:7
x
is an irrefutable pattern because it matches all possible values of type i32
.
let x = 42;
5.1:8
core::option::Option::Some(y)
is a refutable pattern because it does not match value
when
value
denotes core::option::Option::None
.
if let core::option::Option::Some(y) = value {
5.1.1. Identifier Patterns¶
Syntax
IdentifierPattern
::= ref? mut?Binding
BoundPattern
?BoundPattern
::= @Pattern
Legality Rules
5.1.1:1 An identifier pattern is a pattern that binds the value it matches to a binding.
5.1.1:2 A bound pattern is a pattern that imposes a constraint on a related identifier pattern.
5.1.1:3
An identifier pattern yields a binding. An identifier pattern
with keyword mut
yields a mutable binding.
5.1.1:4
An identifier pattern with keyword ref
is a
reference identifier pattern.
5.1.1:5 The identifier pattern enters its binding into binding scope in the value namespace if it does not resolve to a constant, a unit struct constant or a unit enum variant.
5.1.1:6 It is a static error if the identifier pattern consists of anything other than a binding when the binding resolves to a constant, a unit struct constant, or a unit enum variant.
5.1.1:7 It is a static error if the binding of an identifier pattern resolves to a tuple struct or a tuple enum variant.
5.1.1:8 An identifier pattern is an irrefutable pattern when:
5.1.1:9 It has a bound pattern and the bound pattern is an irrefutable pattern, or
5.1.1:10 It does not have a bound pattern and its binding resolves to an irrefutable constant.
5.1.1:11 It does not have a bound pattern and its binding does not resolve to a constant, a unit struct constant, or a unit enum variant.
5.1.1:12 If the identifier pattern does not have a bound pattern, then the type of its binding is determined as follows:
5.1.1:13 If the identifier pattern has only keyword
ref
, then the type is& inferred_type
, whereinferred_type
is the type determined by type inference.5.1.1:14 If the identifier pattern has keywords
ref
mut
, then the type is&mut inferred_type
, whereinferred_type
is the type determined by type inference.5.1.1:15 Otherwise the type is
inferred_type
, whereinferred_type
is the type determined by type inference.
5.1.1:16 If the identifier pattern has a bound pattern, then the type of its binding is determined as follows:
5.1.1:17 If the identifier pattern has only keyword
ref
, then the type& bound_pattern_type
, wherebound_pattern_type
is the type of the bound pattern.5.1.1:18 If the identifier pattern has keywords
ref mut
, then the type is&mut bound_pattern_type
, wherebound_pattern_type
is the type of the bound pattern.5.1.1:19 Otherwise the type is
inferred_type
, whereinferred_type
is the type determined by type inference.
Examples
5.1.1:20 An identifier pattern in the context of a let expression.
let x = 42;
5.1.1:21 An identifier pattern with a bound pattern in the context of a match expression.
match x {
small @ 1 ..= 5 => (),
_ => (),
}
5.1.2. Literal Patterns¶
Syntax
LiteralPattern
::=BooleanLiteral
|ByteLiteral
|ByteStringLiteral
|CharacterLiteral
| -?NumericLiteral
|RawByteStringLiteral
|RawStringLiteral
|SimpleStringLiteral
Legality Rules
5.1.2:1 A literal pattern is a pattern that matches a literal.
5.1.2:2 A numeric literal pattern is a pattern that matches a numeric literal.
5.1.2:3
A numeric literal pattern‘s numeric literal value shall not be a IEEE
floating-point NaN
.
5.1.2:4 The type of a literal pattern is the type of the specified literal.
Examples
5.1.2:5
Two literal patterns in the context of a match expression.
See 5.1.1:20 for the declaration of x
.
match x {
-2 => (),
36 => (),
_ => (),
}
5.1.3. Parenthesized Patterns¶
Syntax
ParenthesizedPattern
::= (Pattern
)
Legality Rules
5.1.3:1 A parenthesized pattern is a pattern that controls the precedence of its subpatterns.
5.1.3:2 A parenthesized pattern is an irrefutable pattern when its nested pattern is an irrefutable pattern.
5.1.3:3 The type of a parenthesized pattern is the type of its nested pattern.
Examples
5.1.3:4
See 5.1.1:20 for the declaration of x
.
let ref_x = &x;
5.1.3:5 A parenthesized pattern inside a reference pattern in the context of a match expression.
match ref_x {
&(1 ..= 5) => (),
_ => (),
}
5.1.4. Path Patterns¶
Syntax
PathPattern
::=PathExpression
|QualifiedPathExpression
Legality Rules
5.1.4:1 A path pattern is a pattern that matches a constant, a unit enum variant, or a unit struct constant indicated by a path.
5.1.4:2 A path pattern expressed as a path expression shall refer to either an associated constant, or a constant.
5.1.4:3 When a path pattern refers to an associated constant or a constant, the associated constant or constant shall not be of a union type.
5.1.4:4 When a path pattern refers to an associated constant or a constant, the type of the associated constant or constant shall be structurally equal.
5.1.4:5
When the type of the path pattern is of an enum type or
struct type, then the enum type or struct type shall be subject
to attribute derive
with arguments core::cmp::Eq
and
core::cmp::PartialEq
.
5.1.4:6 A path pattern expressed as a qualified path expression shall refer to an associated constant.
5.1.4:7 A path pattern is an irrefutable pattern when it refers to:
5.1.4:8 An irrefutable constant,
5.1.4:9 An enum variant of an enum type with zero or more visible empty enum variants and one non-visible empty enum variant where the types of all fields are not subject to visible emptiness
5.1.4:10 A struct where the types of all fields are irrefutable patterns.
5.1.4:11 The type of a path pattern is the type of the constant, unit enum variant, or unit struct constant the path resolved to.
Examples
mod module {
pub const ZERO: i32 = 0;
}
enum Enum { Variant }
5.1.4:12
See 5.1.1:20 for the declaration of x
.
match x {
module::ZERO => (),
Enum::Variant => (),
_ => (),
}
5.1.5. Range Patterns¶
Syntax
RangePattern
::=HalfOpenRangePattern
|InclusiveRangePattern
|ExclusiveRangePattern
|ObsoleteRangePattern
HalfOpenRangePattern
::=RangePatternLowBound
..InclusiveRangePattern
::=RangePatternLowBound
..=RangePatternHighBound
ExclusiveRangePattern
::=RangePatternLowBound
..RangePatternHighBound
ObsoleteRangePattern
::=RangePatternLowBound
...RangePatternHighBound
RangePatternLowBound
::=RangePatternBound
RangePatternHighBound
::=RangePatternBound
RangePatternBound
::=ByteLiteral
|CharacterLiteral
| -?NumericLiteral
|PathExpression
|QualifiedPathExpression
Legality Rules
5.1.5:1 A range pattern is a pattern that matches values which fall within a range.
5.1.5:2 A half-open range pattern is a range pattern with only a range pattern low bound.
5.1.5:3 An inclusive range pattern is a range pattern with both a range pattern low bound and a range pattern high bound.
5.1.5:4 An exclusive range pattern is a range pattern with both a range pattern low bound and a range pattern high bound.
5.1.5:5 An obsolete range pattern is a range pattern that uses obsolete syntax to express an inclusive range pattern.
5.1.5:6 A range pattern bound is a constraint on the range of a range pattern.
5.1.5:7 A range pattern low bound is a range pattern bound that specifies the start of a range.
5.1.5:8 A range pattern high bound is a range pattern bound that specifies the end of a range.
5.1.5:9 A half-open range pattern shall appear within a parenthesized pattern when the context is a slice pattern.
5.1.5:10 The range pattern low bound of an inclusive range pattern shall be less than or equal to its range pattern high bound.
5.1.5:11 The range pattern low bound of an exclusive range pattern shall be less than its range pattern high bound.
5.1.5:12 An obsolete range pattern is equivalent to an inclusive range pattern.
5.1.5:13 A range pattern is an irrefutable pattern only when it spans the entire set of possible values of a type.
5.1.5:14 The types of the range pattern low bound and the range pattern high bound of a range pattern shall be unifiable.
5.1.5:15 The type of a range pattern is determined as follows:
5.1.5:16 If the range pattern is expressed as an inclusive range pattern, an exclusive range pattern, or an obsolete range pattern, then the type is the unified type of the types of the range pattern low bound and the range pattern high bound.
5.1.5:17 Otherwise the type is the type of the range pattern low bound.
5.1.5:18 A path expression of a range pattern shall refer to a constant of a scalar type.
5.1.5:19 A qualified path expression of a range pattern shall refer to an associated constant of a scalar type.
Examples
5.1.5:20
Two range patterns in the context of a match expression.
See 5.1.1:20 for the declaration of x
.
match x {
-30 ..= 2 => (),
57 .. => (),
_ => (),
}
5.1.6. Reference Patterns¶
Syntax
ReferencePattern
::= & mut?PatternWithoutRange
Legality Rules
5.1.6:1 A reference pattern is a pattern that dereferences a pointer that is being matched.
5.1.6:2 A reference pattern is an irrefutable pattern when its nested pattern itself is an irrefutable pattern.
5.1.6:3 The type of a reference pattern is determined as follows:
5.1.6:4 If the reference pattern appears with keyword
mut
, then the type is&mut pattern_without_range_type
, wherepattern_without_range_type
is the type of thePatternWithoutRange
.5.1.6:5 If the reference pattern appears without keyword
mut
, then the type is& pattern_without_range_type
, wherepattern_without_range_type
is the type of thePatternWithoutRange
.
Examples
5.1.6:6
A reference pattern in the context of a match expression.
See 5.1.3:4 for the declaration of ref_x
.
match ref_x {
&23 => (),
_ => (),
}
5.1.7. Rest Patterns¶
Syntax
RestPattern
::=
..
Legality Rules
5.1.7:1 A rest pattern is a pattern that matches zero or more elements that have not already been matched.
5.1.7:2 A rest pattern shall appear at most once within a slice pattern, an identifier pattern of a slice pattern, a tuple pattern, and a tuple struct pattern.
5.1.7:3 A rest pattern is an irrefutable pattern.
5.1.7:4 If a rest pattern appears within a slice pattern or the identifier pattern of a slice pattern, then the type of the rest pattern is determined as follows:
5.1.7:5 If the type of the slice pattern is an array type, then the type is
[T; N]
whereT
is the element type of the array type, andN
is the array type's size minus the number of matched elements of the slice pattern.5.1.7:6 If the type of the slice pattern is a slice type, then the type is that slice type.
Examples
5.1.7:7 A rest pattern in an identifier pattern of a slice pattern, followed by a rest pattern in a slice pattern.
match slice {
[1, 5, .., 7] => (),
[start, end @ ..] => (),
}
5.1.7:8 Rest patterns in tuple patterns.
match tuple { (1, .., y) => (), (.., 5) => (), (..) => (), }
5.1.8. Slice Patterns¶
Syntax
SlicePattern
::= [PatternList
? ]
Legality Rules
5.1.8:1 A slice pattern is a pattern that matches arrays of fixed size and slices of dynamic size.
5.1.8:2 A slice pattern is an irrefutable pattern when it refers to:
5.1.8:3 An array, where each subpattern is an irrefutable pattern, or
5.1.8:4 A slice, where the
PatternList
consists of a single rest pattern, or a single possibly nested identifier pattern whose last bound pattern is a rest pattern.
5.1.8:5 The type of a slice pattern is the same as the expected type.
Examples
let v = vec![1, 2, 3];
5.1.8:6 A slice pattern in the context of a match expression.
match v.as_slice() { [a, b, c] => (), _ => () }
5.2. Struct Patterns¶
Syntax
StructPattern
::=RecordStructPattern
|TupleStructPattern
Deconstructee
::=PathExpression
Legality Rules
5.2:1 A struct pattern is a pattern that matches an enum value, a struct value, or a union value.
5.2:2 A deconstructee indicates the enum variant or type that is being deconstructed by a struct pattern.
5.2:3 A struct pattern is interpreted based on the deconstructee. It is a static error if a struct pattern cannot be interpreted.
5.2:4 A struct pattern is an irrefutable pattern if
5.2:5 All of its subpatterns are irrefutable patterns, and
5.2:6 The deconstructee is either:
5.2:7 A struct type or a union type, or
5.2:8 An enum variant of an enum type with zero or more visible empty enum variants and one non-visible empty enum variant where the types of all fields are not subject to visible emptiness.
5.2.1. Record Struct Patterns¶
Syntax
RecordStructPattern
::=Deconstructee
{RecordStructPatternContent
? }RecordStructPatternContent
::=RecordStructRestPattern
|FieldDeconstructorList
(,RecordStructRestPattern
| ,?)RecordStructRestPattern
::=OuterAttributeOrDoc
*RestPattern
FieldDeconstructorList
::=FieldDeconstructor
(,FieldDeconstructor
)*FieldDeconstructor
::=OuterAttributeOrDoc
* (IndexedDeconstructor
|NamedDeconstructor
|ShorthandDeconstructor
)IndexedDeconstructor
::=FieldIndex
:Pattern
NamedDeconstructor
::=Identifier
:Pattern
ShorthandDeconstructor
::= ref? mut?Binding
FieldIndex
::=DecimalLiteral
Legality Rules
5.2.1:1 A record struct pattern is a pattern that matches a enum variant value, a struct value, or a union value.
5.2.1:2 The deconstructee of a record struct pattern shall resolve to an enum variant, a struct type, or a union type.
5.2.1:3 An indexed deconstructor is a construct that matches the position of a field.
5.2.1:4 An indexed deconstructor matches a field of the deconstructee when its field index and the position of the field in the deconstructee are the same. Such an indexed deconstructor is a matched indexed deconstructor.
5.2.1:5 The type of a matched indexed deconstructor and the type of the matched field shall be unifiable.
5.2.1:6 A named deconstructor is a construct that matches the name of a field.
5.2.1:7 A named deconstructor matches a field of the deconstructee when its identifier and the name of the field are the same. Such a named deconstructor is a matched named deconstructor.
5.2.1:8 The type of a matched named deconstructor and the type of the matched field shall be unifiable.
5.2.1:9 A shorthand deconstructor is a construct that matches the name of a field and binds the value of the matched field to a binding.
5.2.1:10
A shorthand deconstructor with keyword mut
yields a
mutable binding.
5.2.1:11
It is a static error if a shorthand deconstructor has only keyword
ref
or keywords ref
mut
, and its binding shadows a
constant, a unit enum variant, or a unit struct constant.
5.2.1:12 A shorthand deconstructor is equivalent to a named deconstructor where the name of the shorthand deconstructor denotes the identifier of the named deconstructor and the entire content of the shorthand deconstructor denotes the pattern of the named deconstructor.
5.2.1:13 A shorthand deconstructor matches a field of the deconstructee when its name and the name of the field are the same. Such a shorthand deconstructor is a matched shorthand deconstructor.
5.2.1:14 The type of a matched shorthand deconstructor and the type of the matched field shall be unifiable.
5.2.1:15 If the deconstructee of a record struct pattern is a record enum variant or a record struct, then
5.2.1:16 For each field of the deconstructee, the record struct pattern shall either:
5.2.1:17 Contain at most one matched named deconstructor, or
5.2.1:18 Contain at most one matched shorthand deconstructor, or
5.2.1:19 Have exactly one
RecordStructRestPattern
.
5.2.1:20 A
RecordStructRestPattern
is allowed even if all fields of the deconstructee have been matched.
5.2.1:21 If the deconstructee of a record struct pattern is a tuple enum variant or a tuple struct type, then
5.2.1:22 For each field of the deconstructee, the record struct pattern shall either:
5.2.1:23 Contain at most one matched indexed deconstructor, or
5.2.1:24 Have exactly one
RecordStructRestPattern.
5.2.1:25 A
RecordStructRestPattern
is allowed even if all fields of the deconstructee have been matched.
5.2.1:26 If the deconstructee of a record struct pattern is a union type, then
5.2.1:27 The
RecordStructPatternContent
of the record struct pattern shall contain exactly oneFieldDeconstructor
.5.2.1:28 The record struct pattern shall not contain a
RecordStructRestPattern
.5.2.1:29 For the single field of the deconstructee, the record struct pattern shall either:
5.2.1:30 Contain exactly one matched named deconstructor, or
5.2.1:31 Contain exactly one matched shorthand deconstructor.
5.2.1:32 The record struct pattern shall require unsafe context.
5.2.1:33
If the deconstructee of a record struct pattern is a
unit enum variant or a unit struct, then the
record struct pattern shall have at most one RecordStructRestPattern
.
Undefined Behavior
5.2.1:34 It is undefined behavior reading the field of a deconstructee that is a union type when the field contains data that is invalid for the field‘s type.
Examples
5.2.1:35 A record struct pattern deconstructing a record struct.
struct RecordStruct {
first : u32,
second: u32,
}
let record_struct_value = RecordStruct { first: 11, second: 22 };
match record_struct_value {
RecordStruct { second: 33, ref first } => (),
RecordStruct { first: 44, .. } => (),
RecordStruct { .. } => (),
}
5.2.1:36 A record struct pattern deconstructing a tuple struct.
struct TupleStruct (
u32,
u32,
);
let tuple_struct_value = TupleStruct { 0: 11, 1: 22 };
match tuple_struct_value {
TupleStruct { 1: 33, 0: 44 } => (),
TupleStruct { 0: 55, .. } => (),
TupleStruct { .. } => (),
}
5.2.1:37 A record struct pattern deconstructing a union.
union Union {
first : u32,
second: u32,
}
let union_value = Union { second: 11 };
unsafe {
match union_value {
Union { first: 22 } => (),
Union { second: 33 } => (),
_ => (),
}
}
5.2.2. Tuple Struct Patterns¶
Syntax
TupleStructPattern
::=Deconstructee
(PatternList
? )
Legality Rules
5.2.2:1 A tuple struct pattern is a pattern that matches a tuple enum variant value, or a tuple struct value.
5.2.2:2 The deconstructee of a tuple struct pattern shall resolve to a tuple enum variant or a tuple struct type.
5.2.2:3 A subpattern of a tuple struct pattern matches a field of the deconstructee when its position and the position of the field in the deconstructee are the same. Such a subpattern is a matched tuple struct subpattern.
5.2.2:4 The position of a subpattern is determined as follows:
5.2.2:5 If the tuple struct pattern has a
RecordStructRestPattern
, then5.2.2:6 If the subpattern precedes the
RecordStructRestPattern
, then its position is the position within thePatternList
in left-to-right order, starting from zero.5.2.2:7 If the subpattern succeeds the
RecordStructRestPattern
, then its position is the position within thePatternList
list in right-to-left order, starting from the arity of the deconstructee minus one.
5.2.2:8 Otherwise the position is the position within the
PatternList
in left-to-right order, starting from zero.
5.2.2:9 The type of the subpattern of a tuple struct pattern and the type of the matched field shall be unifiable.
5.2.2:10 For each field of the deconstructee, the tuple struct pattern shall either:
5.2.2:11 Contain at most one matched tuple struct subpattern, or
5.2.2:12 Have exactly one
RecordStructRestPattern
.
5.2.2:13
A RecordStructRestPattern
is allowed even if all fields of the
deconstructee have been matched.
Examples
5.2.2:14
See 5.2.1:36 for the declarations of TupleStruct
and
tuple_struct_value
.
match tuple_struct_value {
TupleStruct ( 11, 22 ) => (),
TupleStruct ( 33, .., 44 ) => (),
TupleStruct ( .., 55 ) => (),
TupleStruct ( 66, .. ) => (),
TupleStruct ( .. ) => (),
}
5.2.3. Tuple Patterns¶
Syntax
TuplePattern
::= (PatternList
? )
Legality Rules
5.2.3:1 A tuple pattern is a pattern that matches a tuple which satisfies all criteria defined by its subpatterns.
5.2.3:2 A tuple pattern is an irrefutable pattern when all of its subpatterns are irrefutable patterns.
5.2.3:3 The type of a tuple pattern is the type of the tuple being destructured.
5.2.3:4 A subpattern of a tuple pattern matches a tuple field of the tuple type when its position and the position of the tuple field in the tuple type are the same. Such a subpattern is a matched tuple subpattern.
5.2.3:5 The position of a subpattern is determined as follows:
5.2.3:6 If the tuple pattern has a
RestPattern
, then5.2.3:7 If the subpattern precedes the
RestPattern
, then its position is the position within thePatternList
in left-to-right order, starting from zero.5.2.3:8 If the subpattern succeeds the
RestPattern
, then its position is the position within thePatternList
list in right-to-left order, starting from the arity of the tuple type minus one.
5.2.3:9 Otherwise the position is the position within the
PatternList
in left-to-right order, starting from zero.
5.2.3:10 The type of the subpattern of a tuple pattern and the type of the matched tuple field shall be unifiable.
5.2.3:11 For each tuple field of the tuple type, the tuple pattern shall either:
5.2.3:12 Contain at most one matched tuple subpattern, or
5.2.3:13 Have exactly one
RestPattern
.
5.2.3:14
A RestPattern
is allowed even if all tuple fields of the
tuple type have been matched.
Examples
5.2.3:15 A tuple pattern in the context of a let statement.
let pair = (1, "two");
let (first, second) = pair;
5.2.4. Underscore Patterns¶
Syntax
UnderscorePattern
::=
_
Legality Rules
5.2.4:1 An underscore pattern is a pattern that matches any single value.
5.2.4:2 An underscore pattern is an irrefutable pattern.
5.2.4:3 The type of an underscore pattern is the type of the value it matches.
Examples
5.2.4:4
An underscore pattern in the context of a let statement.
See 5.2.3:15 for the declaration of pair
.
let (first, _) = pair;
5.3. Binding Modes¶
Syntax
Binding
::=Name
Legality Rules
5.3:1 A binding pattern is either an identifier pattern or a shorthand deconstructor.
5.3:2 A binding of a binding pattern binds a matched value to a name.
5.3:3 A binding with binding mode by value binds the matched value by passing the value to the place indicated by the name.
5.3:4 A binding with binding mode by reference binds an immutable reference to the matched value to the name.
5.3:5 A binding with binding mode by mutable reference binds a mutable reference to the matched value to the name.
5.3:6 A non-reference pattern is any pattern except non-binding patterns, path patterns, reference patterns, and underscore patterns.
5.3:7
If a binding pattern does not explicitly specify keyword ref
,
keyword mut
, or keywords ref mut
, then its binding mode
uses the current binding mode of pattern matching.
5.3:8 Initially, the binding mode of a binding is by value.
5.3:9 During the process of pattern matching, each time a reference is matched against a non-reference pattern, the reference is dereferenced and the binding mode is updated as follows:
5.3:10 If the reference is an immutable reference, then the binding mode is updated to by reference.
5.3:11 If the reference is a mutable reference and the binding mode is by value, then the binding mode is updated to by mutable reference.
5.3:12 The process repeats if the dereferenced value is a reference.
Dynamic Semantics
5.3:13 A binding patterns binds its binding to a matched value as follows:
5.3:14 If the binding mode is by reference or the binding pattern appears only with keyword
ref
, then the binding is bound to a reference of the matched value.5.3:15 If the binding mode is by mutable reference or the binding pattern appears with keywords
ref
mut
, then the binding is bound to a mutable reference of the matched value.5.3:16 If the binding mode is by value, then the binding is bound to a copy of the matched value if its type implements the
core::marker::Copy
trait, otherwise the binding is bound to the move of the matched value.
5.4. Pattern Matching¶
Legality Rules
5.4:1 Pattern matching that involves a pattern and a context value proceeds as follows:
5.4:2 For each pattern-without-alternation of the pattern:
5.4:3 If the pattern-without-alternation is an identifier pattern, then perform identifier pattern matching.
5.4:4 If the pattern-without-alternation is a literal pattern, then perform literal pattern matching.
5.4:5 If the pattern-without-alternation is a parenthesized pattern, then perform parenthesized pattern matching.
5.4:6 If the pattern-without-alternation is a path pattern, then perform path pattern matching.
5.4:7 If the pattern-without-alternation is a range pattern, then perform range pattern matching.
5.4:8 If the pattern-without-alternation is a reference pattern, then perform reference pattern matching.
5.4:9 If the pattern-without-alternation is a slice pattern, then perform slice pattern matching.
5.4:10 If the pattern-without-alternation is a record struct pattern, then perform record struct pattern matching.
5.4:11 If the pattern-without-alternation is a tuple struct pattern, then perform tuple struct pattern matching.
5.4:12 If the pattern-without-alternation is a tuple pattern, then perform tuple pattern matching.
5.4:13 If the pattern-without-alternation is an underscore pattern, then perform underscore pattern matching.
5.4:14 Otherwise pattern matching fails.
5.4:15 Only the bindings of a matched pattern-without-alternation are introduced into a binding scope.
5.4.1. Identifier Pattern Matching¶
Legality Rules
5.4.1:1
An identifier pattern with keyword mut
shall require that the
context value is a mutable place expression.
Dynamic Semantics
5.4.1:2 Identifier pattern matching proceeds as follows:
5.4.1:3 If the identifier pattern has a bound pattern, then
5.4.1:4 Performed pattern matching with the bound pattern and the same context value.
5.4.1:5 If matching the bound pattern fails, then matching fails.
5.4.1:6 The context value is bound to the binding of the identifier pattern according to the binding mode.
5.4.1:7 Matching succeeds.
5.4.2. Literal Pattern Matching¶
Dynamic Semantics
5.4.2:1 Literal pattern matching proceeds as follows:
5.4.2:2 If the literal of the literal pattern and the context value are equal, then matching succeeds.
5.4.2:3 Otherwise matching fails.
5.4.3. Parenthesized Pattern Matching¶
Dynamic Semantics
5.4.3:1 Parenthesized pattern matching performs pattern matching with its subpattern and the same context value.
5.4.4. Path Pattern Matching¶
Dynamic Semantics
5.4.4:1 Path pattern matching proceeds as follows:
5.4.4:2 If the constant, unit enum variant or unit struct the path of the path pattern resolved to and the context value are equal, then matching succeeds.
5.4.4:3 Otherwise matching fails.
5.4.5. Range Pattern Matching¶
Dynamic Semantics
5.4.5:1 Range pattern matching proceeds as follows:
5.4.5:2 If the range pattern is expressed as a half-open range pattern and the context value is in the inclusive range from the range pattern low bound to the maximum value of the range pattern low bound‘s type, then matching succeeds.
5.4.5:3 If the range pattern is expressed as either an inclusive range pattern or an obsolete range pattern and the context value is in the inclusive range from the range pattern low bound to the range pattern high bound, then matching succeeds.
5.4.5:4 If the range pattern is expressed as an exclusive range pattern and the context value is in the exclusive range from the range pattern low bound to the range pattern high bound, then matching succeeds.
5.4.5:5 Otherwise matching fails.
5.4.6. Reference Pattern Matching¶
Dynamic Semantics
5.4.6:1 Reference pattern matching proceeds as follows:
5.4.6:2 Dereference the context value.
5.4.6:3 Perform pattern matching with its subpattern and the dereferenced value.
5.4.7. Slice Pattern Matching¶
Dynamic Semantics
5.4.7:1 Slice pattern matching proceeds as follows:
5.4.7:2 If the expected type is a slice type then,
5.4.7:3 If the number of subpatterns of the slice pattern is greater than the length of the context value, then matching fails.
5.4.7:4 If the number of subpatterns of the slice pattern is less than the size of the context value and one of those subpatterns is not a rest pattern, then matching fails.
5.4.7:5 For each subpattern of the slice pattern:
5.4.7:6 Perform pattern matching with the subpattern and the corresponding value from the context value, ignoring rest patterns.
5.4.7:7 If matching the subpattern fails, then matching fails.
5.4.7:8 Otherwise, if the expected type is an array type, then
5.4.7:9 For each subpattern of the slice pattern:
5.4.7:10 Perform pattern matching with the subpattern and the corresponding value from the context value, ignoring rest patterns.
5.4.7:11 If matching the subpattern fails, then matching fails.
5.4.8. Record Struct Pattern Matching¶
Dynamic Semantics
5.4.8:1 Record struct pattern matching proceeds as follows:
5.4.8:2 If the number of subpatterns of the record struct pattern is less than the number of fields of the context value and one of those subpatterns is not a rest pattern, then matching fails.
5.4.8:3 For each subpattern of the struct pattern
5.4.8:4 If the subpattern is a shorthand deconstructor, then the corresponding field of the context value is bound to the binding of the shorthand deconstructor according to the binding mode.
5.4.8:5 Otherwise perform pattern matching with the subpattern and the corresponding field from the context value, ignoring rest patterns.
5.4.8:6 If matching the subpattern fails, then matching fails.
5.4.9. Tuple Struct Pattern Matching¶
Dynamic Semantics
5.4.9:1 Tuple struct pattern matching proceeds as follows:
5.4.9:2 For each subpattern of the tuple struct pattern
5.4.9:3 Otherwise perform pattern matching with the subpattern and the corresponding field from the context value, ignoring rest patterns.
5.4.9:4 If matching the subpattern fails, then matching fails.
5.4.10. Tuple Pattern Matching¶
Dynamic Semantics
5.4.10:1 Tuple pattern matching proceeds as follows:
5.4.10:2 For each subpattern of the tuple pattern
5.4.10:3 Perform pattern matching with the subpattern and the corresponding field from the context value, ignoring rest patterns.
5.4.10:4 If matching the subpattern fails, then matching fails.
5.4.11. Underscore Pattern Matching¶
Dynamic Semantics
5.4.11:1 Underscore pattern matching proceeds as follows:
5.4.11:2 The context value is matched unconditionally.
5.4.11:3 Matching succeeds.