match
statement
Syntax
MatchStatement :
match
Expression{
( Pattern=>
{
Statement*}
)+
}
Pattern :
PatternElem (|
PatternElem )*PatternElem :
IDENTIFIER | BOOLEAN_LITERAL |_
|..
| Path |
Path(
TuplePatterns?)
|(
TuplePatterns?)
|
Path{
StructPatterns?}
TuplePatterns :
Pattern (,
Pattern )*StructPatterns :
Field (,
Field)*(,
..
)?Field :
IDENTIFIER:
Pattern
A match
statements compares expression
with patterns, then executes body of the matched arm.
Example:
enum MyEnum {
Unit
Tuple(u32, u256, bool)
}
contract Foo {
pub fn bar(self) -> u256 {
let val: MyEnum = MyEnum::Tuple(1, 10, false)
return self.eval(val)
}
fn eval(self, val: MyEnum) -> u256 {
match val {
MyEnum::Unit => {
return 0
}
MyEnum::Tuple(.., false) => {
return 1
}
MyEnum::Tuple(a, b, true) => {
return u256(a) + b
}
}
}
}