为什么在使用非文字模式时无法访问此匹配模式?
Why is this match pattern unreachable when using non-literal patterns?
以下代码(playground)
let max_column = 7;
edge = match current_column {
0 => Edge::Left,
max_column => Edge::Right,
_ => Edge::NotAnEdge
};
导致以下警告:
warning: unreachable pattern
--> src/main.rs:10:9
|
9 | max_column => Edge::Right,
| ---------- matches any value
10 | _ => Edge::NotAnEdge
| ^ unreachable pattern
|
= note: #[warn(unreachable_patterns)] on by default
用文字替换变量 max_column
效果很好:
let max_column = 7;
edge = match current_column {
0 => Edge::Left,
7 => Edge::Right,
_ => Edge::NotAnEdge
};
为什么 _
在第一个示例中无法访问,而对于 current_column != max_column
中的任何值都可以达到?
The Rust Programming Language 解释了如何处理 match
表达式,强调我的:
When the match
expression executes, it compares the resulting value against the pattern of each arm, in order.
在您的示例中,max_column
是要绑定的变量的名称,不是 常量或外部变量。当编译器达到 max_column
时,任何剩余的值都将分配给该匹配臂,从而使后续臂无法到达。
在你的例子中,你想让 max_column
成为一个实常数:
let current_column = 1;
const MAX_COLUMN: i32 = 7;
edge = match current_column {
0 => Edge::Left,
MAX_COLUMN => Edge::Right,
_ => Edge::NotAnEdge
};
或者,如果这不可能,您需要一个 比赛后卫:
let current_column = 1;
let max_column = 7;
edge = match current_column {
0 => Edge::Left,
a if a == max_column => Edge::Right,
_ => Edge::NotAnEdge
};
请注意,作为第一个近似值,a
和 _
在这种情况下是相同的!在这两种情况下,匹配的变量都将绑定到一个名称(分别为 a
或 _
),但任何以 _
为前缀的标识符都是特殊情况,可用作未使用的变量占位符.
:
_
is a separate special case, it's not a variable binding at all, but it is the absence of one! Matching against _x
moves the value into _x
, _
does no such thing. (The difference is observable.)
以下代码(playground)
let max_column = 7;
edge = match current_column {
0 => Edge::Left,
max_column => Edge::Right,
_ => Edge::NotAnEdge
};
导致以下警告:
warning: unreachable pattern
--> src/main.rs:10:9
|
9 | max_column => Edge::Right,
| ---------- matches any value
10 | _ => Edge::NotAnEdge
| ^ unreachable pattern
|
= note: #[warn(unreachable_patterns)] on by default
用文字替换变量 max_column
效果很好:
let max_column = 7;
edge = match current_column {
0 => Edge::Left,
7 => Edge::Right,
_ => Edge::NotAnEdge
};
为什么 _
在第一个示例中无法访问,而对于 current_column != max_column
中的任何值都可以达到?
The Rust Programming Language 解释了如何处理 match
表达式,强调我的:
When the
match
expression executes, it compares the resulting value against the pattern of each arm, in order.
在您的示例中,max_column
是要绑定的变量的名称,不是 常量或外部变量。当编译器达到 max_column
时,任何剩余的值都将分配给该匹配臂,从而使后续臂无法到达。
在你的例子中,你想让 max_column
成为一个实常数:
let current_column = 1;
const MAX_COLUMN: i32 = 7;
edge = match current_column {
0 => Edge::Left,
MAX_COLUMN => Edge::Right,
_ => Edge::NotAnEdge
};
或者,如果这不可能,您需要一个 比赛后卫:
let current_column = 1;
let max_column = 7;
edge = match current_column {
0 => Edge::Left,
a if a == max_column => Edge::Right,
_ => Edge::NotAnEdge
};
请注意,作为第一个近似值,a
和 _
在这种情况下是相同的!在这两种情况下,匹配的变量都将绑定到一个名称(分别为 a
或 _
),但任何以 _
为前缀的标识符都是特殊情况,可用作未使用的变量占位符.
_
is a separate special case, it's not a variable binding at all, but it is the absence of one! Matching against_x
moves the value into_x
,_
does no such thing. (The difference is observable.)