条件判断
if..elif..else 条件分支
if condition {
# ...
}
可以使用 back
关键字中断 if,返回上一作用域
if condition {
back
# here will not be executed
}
back
关键字可以携带返回值:
<variable-name> := if condition {
back <backed-value>
# here will not be executed
}
# now <variable-name> is defined with <backed-value>
if condition {
# ...
} else {
# ...
}
if condition {
# ...
} elif condition {
# ...
} else {
# ...
}
match 条件分支
match <matched-value> {
<expected-value1>: {
# do something for <expected-value1>
# no need to break
}
<expected-value2>: {
# do something for <expected-value2>
}
_: {
# default resolver
}
}
match 表达式中也可以使用 back
关键字:
<variable-name> = match <matched-value> {
<expected-value1>: {
back <backed-value1>
# here will not be executed
}
<expected-value2>: {
back <backed-value2>
# here will not be executed
}
}
match 表达式中的 if..elif..else 语法糖:
match <matched-value> {
<expected-value>: {
if condition1 {
# do something
} elif condition2 {
# do something
} else {
# do something
}
}
}
等效于:
match <matched-value> {
<expected-value> if condition1 {
# do something
} elif condition2 {
# do something
} else {
# do something
}
}